Issue
I have the below bash script that I need to take two at a time.Total have 8 files in folder. And name of files is discontinuous like:
file5.txt file7.txt file11.txt file13.txt file14.txt file21.txt file22.txt file23.txt
My script like:
caulatepet.py \
--i file5.txt, file7.txt \
--menscor men-code.list \
--stat sex-rd.ratio \
--out ./result/file5_file7.txt
I need to take two file as input at a time (as --i input)
, like permutations and combinations.For example like below:
file5.txt file7.txt
file5.txt file11.txt
file5.txt file13.txt
file5.txt file14.txt
file5.txt file21.txt
file5.txt file22.txt
file5.txt file23.txt
file7.txt file11.txt
file7.txt file13.txt
file7.txt file14.txt
file7.txt file21.txt
file7.txt file22.txt
file7.txt file23.txt
...etc.
How can I do this? Thank you.
Solution
If I understand correctly, you're trying to compare the files in pairs, but not comparing files to themselves (i.e. don't compare file5.txt to file5.txt) and only compare each pair once (i.e. after comparing file5.txt to file7.txt, it's not necessary to compare file7.txt to file5.txt). You should be able to do it by storing the filenames in an array, then making a double loop over the array:
files=(file5.txt file7.txt file11.txt file13.txt file14.txt file21.txt file22.txt file23.txt)
# for i as each index in the array...
for ((i=0; i<${#files[@]}; i++)); do
# for j as each index HIGHER THAN i in the array...
for ((j=i+1; j<${#files[@]}; j++)); do
caulatepet.py \
--i "${files[i]}", "${files[j]}" \
--menscor men-code.list \
--stat sex-rd.ratio \
--out "./result/${files[i]%.txt}_${files[j]}"
done
done
BTW, the syntax of that -i
option looks weird; is there really supposed to be a space after the ,
?
Answered By - Gordon Davisson Answer Checked By - Cary Denson (WPSolving Admin)