How do I use shell script to check if a bucket exists?
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
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