简体   繁体   中英

Pass value of the variable to a command in bash script

How do I pass the value of the variable to a command in a bash script?

Specifically, I want to create a AWS S3 bucket with (partially) random name. This is what I got so far:

#!/bin/bash
random_id=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 8 | head -n 1)
bucket_name=s3://mybucket-$random_id
echo Bucket name: ${bucket_name}
aws s3 mb ${bucket_name}

The output I get:

Bucket name: s3://mybucket-z4nnli2k
 Parameter validation failed:cket-z4nnli2k
": Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$"

The bucket name is generated correctly, but aws s3 mb ${bucket_name} fails. If I just run aws s3 mb s3://mybucket-z4nnli2k then the bucket is created, so I assume that aws s3 mb ${bucket_name} is not the correct way to pass the value of the bucket_name to the aws s3 mb command.

It must be something obvious but I have almost zero experience with shell scripts and can't figure it out.

How do I pass the value of bucket_name to the aws s3 mb command?

Thanks to the comments above, this is what I got working:

#!/bin/bash
random_id=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 8 | head -n 1)
bucket_name=s3://mybucket-$random_id
echo Bucket name: ${bucket_name}
aws s3 mb "${bucket_name}"

I also had to run dos2unix on the script, apparently there were bad line breaks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM