If statement in build using ymal aws - amazon-web-services

Hey I'm trying to do an if statement in Yaml, something like
if $NUMBER_OF_SOURCES == 3 then echo 1
(echo 1 will change I the future to a script that does something in aws )
what's the correct syntax?
is it even possible?
Hey I'm trying to do an if statement in Yaml,
something like this:
if
$NUMBER_OF_SOURCES == 3 then echo 1
(echo 1 will change I the future to a script that does something in aws ),
what's the correct syntax? is it even possible?
tried to do something like that but i get
- if [ $NUMBER_OF_SOURCES -eq 3 ]
- then
- echo "true"
- fi
[Container] 2022/04/24 10:35:36 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: if [ $NUMBER_OF_SOURCES -eq 3 ]. Reason: exit status 2

You do this using pipe (|) notation in yaml:
- |
if [ $NUMBER_OF_SOURCES -eq 3 ]
then
echo "true"
fi
or just put everything in one line:
- if [ $NUMBER_OF_SOURCES -eq 3 ]; then echo "true"; fi

Related

How to Check if AWS Named Configure profile exists

How do I check if a named profile exists before I attempt to use it ?
aws cli will throw an ugly error if I attempt to use a non-existent profile, so I'd like to do something like this :
$(awsConfigurationExists "${profile_name}") && aws iam list-users --profile "${profile_name}" || echo "can't do it!"
Method 1 - Check entries in the .aws/config file
function awsConfigurationExists() {
local profile_name="${1}"
local profile_name_check=$(cat $HOME/.aws/config | grep "\[profile ${profile_name}]")
if [ -z "${profile_name_check}" ]; then
return 1
else
return 0
fi
}
Method 2 - Check results of aws configure list , see aws-cli issue #819
function awsConfigurationExists() {
local profile_name="${1}"
local profile_status=$( (aws configure --profile ${1} list) 2>&1)
if [[ $profile_status = *'could not be found'* ]]; then
return 1
else
return 0
fi
}
usage
$(awsConfigurationExists "my-aws-profile") && echo "does exist" || echo "does not exist"
or
if $(awsConfigurationExists "my-aws-profile"); then
echo "does exist"
else
echo "does not exist"
fi
I was stuck with the same problem and the proposed answer did not work for me.
Here is my solution with aws-cli/2.8.5 Python/3.9.11 Darwin/21.6.0 exe/x86_64 prompt/off:
export AWS_PROFILE=localstack
aws configure list-profiles | grep -q "${AWS_PROFILE}"
if [ $? -eq 0 ]; then
echo "AWS Profile [$AWS_PROFILE] already exists"
else
echo "Creating AWS Profile [$AWS_PROFILE]"
aws configure --profile $AWS_PROFILE set aws_access_key_id test
aws configure --profile $AWS_PROFILE set aws_secret_access_key test
fi

Why [ ... ] syntax doesn't work in a Makefile while `test` does?

I am on Linux Mint 19. I am entirely new to Makefiles.
Here is the problematic part:
[ $(shell id --user) -eq 0 ] && ( echo && echo "distrib target has to be run as normal user" && echo && exit 1 )
which throws this error:
[ 1000 -eq 0 ] && ( echo && echo "distrib target has to be run as normal user" && echo && exit 1 )
Makefile:25: recipe for target 'distrib' failed
make: *** [distrib] Error 1
On the contrary, using test command directly proves to be working entirely:
if test $(shell id --user) -eq 0; then ( echo && echo "distrib target has to be run as normal user" && echo && exit 1 ) fi
I want to ask why that is, did I break some Makefile rule?
This doesn't have anything to do with makefiles, it has to do with shell scripting and the difference between using && vs. if in terms of the exit code. You are comparing apples and oranges here.
It's not related to test vs [. If you write the version using [ inside an if statement you'll get the same behavior as you do with test, and if you write the test version with the && model you'll get the same behavior as you do with [.
Run this in your shell:
[ 1000 -eq 0 ] && echo hi
echo $?
Now run this in your shell:
if [ 1000 -eq 0 ]; then echo hi; fi
echo $?
You'll see the former gives a non-0 exit code, while the latter gives a 0 (success) exit code. That's how if works; it "swallows" the exit code of the condition.
Make always looks at the exit code of the shell script to decide if it failed or not.
Generally in make scripting you want to re-arrange your expressions to use || rather than &&. That ensures that if the script exits early it exits with a success code not a failure code. You can write your script like this:
[ $$(id -u) -ne 0 ] || ( echo && echo "distrib target has to be run as normal user" && echo && exit 1 )
Note I use $$(id -u) not $(shell id --user); the recipe is run in the shell already and it's an anti-pattern to use the make shell function in a recipe. Also, the -u option is a POSIX standard option while --user is only available in the GNU utilities version of id.

How to use AWS Data pipeline shellcommandprecondition

My first question here! I've built a Data Pipeline for daily ETL whichs moves and tranforms data between Aurora, Redshift and Hive. All works well however I'm truly stuck on trying to implement a Shellcommandprecondition. The aim is to check the total row count in a view sitting on Aurora MySQL. If the view is empty (0 rows) then Data Pipeline should execute. If there are rows in the view - then the pipeline wait a bit, and then eventually fail after 4 retries.
Can someone help me out with the code for the actual check and query? This is what I've got so far but no luck with it:
#!/bin/bash
count=`mysql -u USER -pPW -h MASTERPUBLIC -p 3306 -D DBNAME -s -N -e "SELECT count(*) from MyView"`
if $count = 0
then exit 0
else exit 1
fi
In the pipeline definition it looks as follows:
{
"retryDelay": "15 Minutes",
"scriptUri": "s3://mybucket/ETLprecondition.bash",
"maximumRetries": "4",
"name": "CheckViewEmpty",
"id": "PreconditionId_pznm2",
"type": "ShellCommandPrecondition"
},
I have very little experience coding so I may be completely off...
Right, a few hours have passed and I finally solved it. There were a few issues holding me up.
Mysql client was not installed on the ec2 instance. Solved that by adding install command
Next issue was that the if $count = 0 line wasn't working as I would have expected it to do with my limited experience. Exchanged it for if [ "$count" -eq "0" ];
Final and working code is:
#!/bin/bash
if type mysql >/dev/null 2>&1; then
count=`mysql -u USER -pPW -h MASTERPUBLIC -p 3306 -D DBNAME -s -N -e "SELECT count(*) from MyView"`
if [ "$count" -eq "0" ];
then exit 0
else exit 1
fi
else
sudo yum install -y mysql
count=`mysql -u USER -pPW -h MASTERPUBLIC -p 3306 -D DBNAME -s -N -e "SELECT count(*) from MyView"`
if [ "$count" -eq "0" ];
then exit 0
else exit 1
fi
fi

Regex Validation Kornshell

I have the following REGEX expression
^(\(?\+?(44|0{1}|0{2}4{2})[1-9]{1}[0-9]{9}\)?)?$
In an attempt to cover all eventuality of mobile number in the UK. While parsing this validation through a REGEX tester online, which works great I am having difficulty getting it to work correctly in cornshell
fn_validate_msisdn() {
MSISDN=$1
REGEX_PTN="^(\(?\+?(44|0{1}|0{2}4{2})[1-9]{1}[0-9]{9}\)?)?$"
if [ `echo $MSISDN | egrep -c $REGEX_PTN` -gt 0 ]
then
return 1
fi
return 0;
}
Being called by:
if [ ! `fn_validate_msisdn ${MSISDN}` ]
then
...
fi
However It always seems to fail, either with illegal syntax or always returning greater than one.
some test data:
447999999999 : OK
07999999999 : OK
4407948777622 : FAIL
43743874874387439843 : FAIL
Any Suggestions would be great
Your function can be just this:
fn_validate_msisdn() {
MSISDN=$1
REGEX_PTN="^(\(?\+?(44|0{1}|0{2}4{2})[1-9]{1}[0-9]{9}\)?)?$"
echo "$MSISDN" | egrep -q "$REGEX_PTN";
}
then:
fn_validate_msisdn 43743874874387439843
echo $?
1
fn_validate_msisdn 447999999999
echo $?
0
Remember return status of 0 means success and 1 means failure here.

How to separate package name by regex in bash?

I'm writing a script function to separate package tar ball name listing into package name version.
xorg-fonts-misc-1.0b-1
Xorg-font-bitstream-75dpi-1.0.0-2.i386
Xorg-font-bitstream-100dpi-1.2a-2.arm
Other-Third-Party-1.2.2-1-any
I'm using the following script to separate name and version.
split_pkgname_pipe() { # split x-x-1.3-1.x -> x-x 1.3-1.x
[ $opt_v != 0 ] && echo "dbg:split_pkgname_pipe $*" >&2
awk '{
f=$0
sub(/\-[0-9].*$/,"")
n=$1
v=substr(f, length(n)+2)
print n, v
}'
}
The problem of my code will cause Xorg-font-bitstream-75dpi-1.0.0 separate as Xorg-font-bitstream and 75dpi-1.0.0. But I want Xorg-font-bitstream-75dpi and -1.0.0
[SOLVED]
split_pkgname_pipe() { # split x-x-1.3-1.x -> x-x 1.3-1.x
[ $opt_v != 0 ] && echo "dbg:split_pkgname_pipe $*" >&2
local line namever name ver rel
while read line ; do
namever="${line%-*}"
rel="${line##*-}"
if [ `expr match $rel '[0-9]'` = 0 ] ; then # rel is 'i386/any'...
name="${namever%-*}"
ver="${namever##*-}"
namever="$name"
rel="$ver-$rel"
fi
name="${namever%-*}"
ver="${namever##*-}"
echo "$name $ver-$rel"
done
}
$ package="Xorg-font-bitstream-75dpi-1.0.0"
$ echo "${package%-*}"
Xorg-font-bitstream-75dpi
$ echo "${package##*-}"
1.0.0
Try this
sed -re '/^(.*?)((\d[a-z]?\.)+.*)$/\1\t\2/gmi' file.txt