Sample Header Ad - 728x90

How to pass each line of an output file as an argument to a for loop in the same bash script?

1 vote
3 answers
1509 views
am trying to write a bash script to get the total size of sub folders in a S3 bucket. My bucketpath s3://path1/path2/subfolders Inside the path2 folder i have many sub-folder like 2019_06 2019_07 2019_08 2019_09 2019_10 2019_11 2019_12 I need to get the size of each subfolder in a bash script. I wrote a script like
#!/bin/bash

FILES=$(mktemp)

aws s3 ls "s3://path1/path2/"  >> "$FILES"

cat $FILES
echo
for file in $FILES
do    
  if [ ! -e "$file" ]      
  then 
    s3cmd du -r  s3://path1/path2/$file
    echo "$file"; echo
    continue               
  fi 
    
  echo
done
The output of cat $tmpfile is as below 2019_06 2019_07 2019_08 2019_09 2019_10 2019_11 2019_12 But am getting error. While passing the variable into the for loop. Ideally my aim is like for each iteration when for loop runs inside do .....The command should be like
s3cmd du -r s3://path1/path2/2019_06

s3cmd du -r s3://path1/path2/2019_07

s3cmd du -r s3://path1/path2/2019_08
etc... So that i can get the total size of the folder Kindly help! **Update** I have edited the code as suggested
#!/bin/bash

FILES=$(mktemp)

aws s3 ls "s3://path1/path2/"  >> "$FILES"
    
for file in cat $FILES
do
  if [ -n "$file" ]
  echo $file
done
Asked by clarie (95 rep)
Apr 29, 2020, 10:06 AM
Last activity: May 26, 2021, 11:33 PM