[英]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? 如何在bash脚本中将变量的值传递给命令?
Specifically, I want to create a AWS S3 bucket with (partially) random name. 具体来说,我想使用(部分)随机名称创建一个AWS S3存储桶。 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. 存储桶名称正确生成,但是
aws s3 mb ${bucket_name}
失败。 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. 如果我只运行
aws s3 mb s3://mybucket-z4nnli2k
那么将创建存储桶,因此我认为aws s3 mb ${bucket_name}
不是将bucket_name
的值bucket_name
给aws s3 mb
命令的正确方法。
It must be something obvious but I have almost zero experience with shell scripts and can't figure it out. 这一定是显而易见的,但是我对shell脚本的经验几乎为零,无法弄清楚。
How do I pass the value of bucket_name
to the aws s3 mb
command? 如何将
bucket_name
的值bucket_name
给aws s3 mb
命令?
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. 我还必须在脚本上运行
dos2unix
,显然有不好的换行符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.