I am executing some API calls in a bash script. In this case if the API call succeeds, then a json
file is returned, if it fails a stderr
is returned. I would like to capture the stderr
and parse it on a fail. After reviewing a lot of answers on SO I found no combination so far that works.
For example, I have added the below rule already, so when I run:
myCmd=("aws --profile myProfile --region eu-west-1 ec2 authorize-security-group-ingress --group-id sg-999aa999 --protocol tcp --port 80 --cidr 0.0.0.0/0 ")
${myCmd[@]} > myJson.file
#check if success
if [ "$?" -ne "0" ]
then
# PARSE STDERR
fi
The success check returns 255 and stderr returns below. So now I would like to parse the message to check if it is a general error or a duplicate:
A client error (InvalidPermission.Duplicate) occurred when calling the AuthorizeSecurityGroupIngress operation: the specified rule "peer: 0.0.0.0/0, TCP, from port: 80, to port: 80, ALLOW" already exists
myCmd=("aws --profile myProfile --region eu-west-1 ec2 authorize-security-group-ingress --group-id sg-999aa999 --protocol tcp --port 80 --cidr 0.0.0.0/0 ")
if "${myCmd[@]}" > myJson.file 2> error.file; then
echo ok
else
err="$(cat error.file)"
# do domething with $err
fi