Below is part of a script to execute a command on a file defined by the variable DSpath. There are several of these files distinguished by the number at the end of the filename “_01”. The variable numlongs define how many of these files are present. What is the best way to write a loop which will execute the command for each of these files using the variables? Thank you.
<code>echo How many LONG files exist?
read numlongs
DSpath="$SAMpath"/"$MRN"_EPI-LONG_"$DOM"_01.ds
exec SAMsrc -r"$DSpath" -cGlobal,3-70Hz -W1 -t"$filename"final.max -v
</code>
<code>echo How many LONG files exist?
read numlongs
DSpath="$SAMpath"/"$MRN"_EPI-LONG_"$DOM"_01.ds
exec SAMsrc -r"$DSpath" -cGlobal,3-70Hz -W1 -t"$filename"final.max -v
</code>
echo How many LONG files exist?
read numlongs
DSpath="$SAMpath"/"$MRN"_EPI-LONG_"$DOM"_01.ds
exec SAMsrc -r"$DSpath" -cGlobal,3-70Hz -W1 -t"$filename"final.max -v
4
Modifying OP’s current code to use a for
loop:
<code>echo How many LONG files exist?
read numlongs
for ((i=1; i<=numlongs; i++))
do
printf -v fNum "%02d" $i # left pad with 0 to generate a 2-digit string
DSpath="${SAMpath}/${MRN_EPI}-LONG_${DOM}_${fNum}.ds"
SAMsrc -r"$DSpath" -cGlobal,3-70Hz -W1 -t"$filename"final.max -v
done
</code>
<code>echo How many LONG files exist?
read numlongs
for ((i=1; i<=numlongs; i++))
do
printf -v fNum "%02d" $i # left pad with 0 to generate a 2-digit string
DSpath="${SAMpath}/${MRN_EPI}-LONG_${DOM}_${fNum}.ds"
SAMsrc -r"$DSpath" -cGlobal,3-70Hz -W1 -t"$filename"final.max -v
done
</code>
echo How many LONG files exist?
read numlongs
for ((i=1; i<=numlongs; i++))
do
printf -v fNum "%02d" $i # left pad with 0 to generate a 2-digit string
DSpath="${SAMpath}/${MRN_EPI}-LONG_${DOM}_${fNum}.ds"
SAMsrc -r"$DSpath" -cGlobal,3-70Hz -W1 -t"$filename"final.max -v
done
2
I ended up writing out the echo file of all the commands to a text file and then using a bash command to execute the commands. This may be a bit round about but it works!
1