I have the below command which works fine when I try to execute via cmd line
s3cmd ls $'bucket/raw_data/filename'
Now , I have lot of file , I try to do the above via shell script, where I take input from a text file.
#!/bin/bash
if [ ! -f "$1" ]; then
echo "Error: Input file not found"
exit 1
fi
while IFS= read -r line; do
s3cmd ls $('bucket/raw_data/%s') "$line"
done < "$1"
I get an error saying path not found and also the dollar sign is resolved to my home path for example – /home/hostname. Also, %s
is not substituted. Dollar and single quote is needed as I have some special character in names.
4
You have to use printf -v
in order to prepare a variable:
#!/bin/bash
if [ ! -f "$1" ]; then
echo "Error: Input file not found"
exit 1
fi
while IFS= read -r line; do
printf -v filename 'bucket/raw_data/%s' "$line"
s3cmd ls "$filename"
done < "$1"
9
You can also use xargs
. If your filenames are in file
and you are not expecting to interpolate values:
xargs -L1 -I% s3cmd ls '"bucket/raw_data/%"' < file
1
I am not familiar with s3, but you command interpolation looks odd :s3cmd ls $('bucket/raw_data/%s') "$line"
.
I think you should try something like this:
#!/bin/bash
if [ ! -f "$1" ]; then
echo "Error: Input file not found"
exit 1
fi
while IFS= read -r line; do
s3cmd ls "bucket/raw_data/$line"
done < "$1"