toy toy - 1 year ago 948
Bash Question

How do I use shell script to check if a bucket exists?

I have

aws
cli installed. I'm just not sure how to do this in shell script.

when I run command
aws s3 ls s3://bucket
it would give me something like this


A client error (NoSuchBucket) occurred when calling the ListObjects operation: The specified bucket does not exist


That means the bucket doesn't exist. So I want to run that from shell script and check if
grep
finds it. But my command doesn't work.

if [ $(aws s3 ls "s3://$S3_BUCKET" | grep 'NoSuchBucket' &> /dev/null) == 0 ]
then
echo "$S3_BUCKET doesn\'t exist please check again"
exit
fi


It just gave me this


backup.sh: 20: [: 0: unexpected operator


Updated

I changed the script to be

echo "S3_BUCKET=$S3_BUCKET"
if aws s3 ls "s3://$S3_BUCKET" | grep -q 'AllAccessDisabled'
then
echo "$S3_BUCKET doesn\'t exist please check again"
exit
fi


And this is the output I got

A client error (AllAccessDisabled) occurred when calling the ListObjects operation: All access to this object has been disabled


So the text contains
AllAccessDisabled
but I still don't the
echo
the next line.

Answer Source

The code you listed wouldn't have given you that error.

If you had written the script without the space between the leading [ and the $( that would have.

Also grep isn't going to output 0 in that case so that test isn't going to work the way you want.

If you want to test whether grep found anything then you want to use the -q argument to grep like this:

if aws s3 ls "s3://$S3_BUCKET" 2>&1 | grep -q 'NoSuchBucket'
then