Quantcast
Channel: Active questions tagged ubuntu - Stack Overflow
Viewing all articles
Browse latest Browse all 6046

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

$
0
0

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"    exitfi

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"    exitfi

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.


Viewing all articles
Browse latest Browse all 6046

Trending Articles