0

I want to assign a variable in shell script for the below aws command.. If the command is successful,I want to assign the output to S3_BUCKET_REGION.Eg: S3_BUCKET_REGION = us-east-1.

S3_BUCKET_REGION=$( aws s3api get-bucket-location --bucket ${TF_STATE_S3_BUCKET} | jq -r '.LocationConstraint // "us-east-1"' )

But if the bucket does not exist,the error for the above command is "An error occurred (NoSuchBucket) when calling the GetBucketLocation operation: The specified bucket does not exist"

I want to capture this error and echo it in the script.

So if the command runs successfully,I want to assign to a variable.If not ,I want to echo the error.How to do conditional statement for this?

1 Answer 1

1

Usually commands sends output to STDOUT and errors to STDERR. $() grabs only STDOUT, so you should finish your command with redirection of STDERR to STDOUT

MYVAR=$( blablabla 2>&1 )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.