简体   繁体   中英

how to iterate bash script to run command?

I'm trying to copy multiple folders in years wise from one s3 bucket to another

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.

Can I create any shell script which runs for months of a years to copy data to bucket_two ?

I'm new in shell script, I know how run loops but place holders using not sure.

Any help?

You can do that with for loops and variable substitution aka parameter expansion. 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. Ex:

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

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