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.
To check whether a bucket already exists before attempting to create one with the same name, call the doesBucketExist method. It will return true if the bucket exists, and false otherwise. if (s3. doesBucketExistV2(bucket_name)) { System.
To delete an S3 bucketSign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, select the option next to the name of the bucket that you want to delete, and then choose Delete at the top of the page.
The s3api head-bucket is more direct and does not incur the expense of listing a bucket with many files.
http://docs.aws.amazon.com/cli/latest/reference/s3api/head-bucket.html
if aws s3api head-bucket --bucket "$S3_BUCKET" 2>/dev/null; then
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With