Reputation: 23
I have created a script that identifies a unique set of files (from a large list) and saves the file paths as unique variables (e.g. $vol1, $vol2, $vol3... see below).
for volume in `seq 1 $num_ap_files`
do
bindex=$(cat ../b0_volumes_tmp.txt | head -$volume | tail -1)
eval 'vol'${volume}=$(cat ../all_volumes.txt | head -$bindex | tail -1)
done
I'd like to concatenate the variables into one string variable to use as input for a separate command line program. As the number of volumes may differ - it seems most appropriate to create a loop of the following nature (??? define where I am having trouble):
??? files=$vol1
for i in `seq 2 $num_ap_files
do
??? eval files=${files}" $vol"${i)
done
I've tried a few different options here using files+=, eval function, etc. and continue to get an error message: -bash: vol0001.nii.gz: command not found. Is there a way around this? The filenames need to be in string format to be fed into subsequent processing steps.
Thanks, David
Upvotes: 2
Views: 61
Reputation: 6134
Use an array for this:
vol=()
for ((volume = 1; volume <= num_ap_files; volume++)); do
bindex=$(sed -n "$volume p" ../b0_volumes_tmp.txt)
vol[$volume]=$(sed -n "$bindex p" ../all_volumes.txt)
done
files="${vol[*]}"
The variable files
contains the content of the array vol
as one single string.
If you now want to call a command with each element of vol
as an argument, no need to use an intermediate variable like files
:
my_next_command "${vol[@]}"
Note: the command sed -n "$N p" file
prints the Nth line of the file (the same as your cat file | head -$N | tail -1
)
Upvotes: 2