简体   繁体   English

如何迭代 bash 脚本来运行命令?

[英]how to iterate bash script to run command?

I'm trying to copy multiple folders in years wise from one s3 bucket to another我试图在几年内将多个文件夹从一个 s3 存储桶复制到另一个存储桶

aws s3 sync s3://bucket_one/2022XX s3://bucket_two/2022XX (XX is 01 to 12)

I've folders under bucket_one like 202201,202202 so on and for other years too.我在bucket_one下有文件夹,例如 202201、202202 等等,其他年份也是如此。

Can I create any shell script which runs for months of a years to copy data to bucket_two ?我可以创建任何运行数月或数年的 shell 脚本来将数据复制到bucket_two吗?

I'm new in shell script, I know how run loops but place holders using not sure.我是 shell 脚本的新手,我知道如何运行循环,但不确定使用占位符。

Any help?有什么帮助吗?

You can do that with for loops and variable substitution aka parameter expansion.您可以使用for循环和变量替换(也称为参数扩展)来做到这一点。 You can read more about it in the official manual here but personaly I find it easier to read something like this .您可以在此处的官方手册中阅读更多相关信息,但我个人觉得阅读这样的内容更容易。

Anyway for your specific case you can try something like this:无论如何,对于您的具体情况,您可以尝试这样的事情:

for month in {01..12}; do
    aws s3 sync s3://bucket_one/2022$month s3://bucket_two/2022$month
done

To debug if something doesn't work you can echo the command out.如果某些东西不起作用,要进行调试,您可以echo显命令。 Ex:前任:

for month in {01..12}; do
    echo "aws s3 sync s3://bucket_one/2022$month s3://bucket_two/2022$month"
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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