user3636476
user3636476

Reputation: 1417

Bash, issue on for loop

I want to list specified files (files uploaded yesterday) from an amazon S3. Then I want to loop on this list, and for every single element of the list I want to unzip the file.

My code is:

for file in s3cmd ls s3://my-bucket/`date +%Y%m%d -d "1 day ago"`*
do s3cmd get $file
arrIN=(${file//my-bucket\//})
gunzip ${arrIN[1]}
done

so basicaly arrIN=(${file//my-bucket//}); explodes my string and allow me to retrieve the name of the file I want to unzip.

Thing is, file are downloading but nothing is being unzip, so I tried:

for file in s3cmd ls s3://my-bucket/`date +%Y%m%d -d "1 day ago"`*
do s3cmd get $file
echo test1
done

Files are being downloaded but nothing is being echo. Loop is just working for the first line...

Upvotes: 0

Views: 346

Answers (1)

chepner
chepner

Reputation: 531738

You need to use command substitution to iterate over the result of the desired s3smd ls command.

for file in $(s3cmd ls s3://my-bucket/$(date +%Y%m%d -d "1 day ago")*); do

However, this isn't the preferred way to iterate over the output of a command, since in theory the results could contain whitespace. See Bash FAQ 001 for the proper method.

Upvotes: 4

Related Questions