0

I don't know if my title is clear. I explain :

I have a python script which needs 3 arguments (3 files). I need to run it three times. So, I could run the python script 3 times separately, but I want to run it only once in a nested loop.

For that, I put my necessary files in three arrays (which correspond to the three arguments). That script will create an output directory.

In my case, I will have three output directories. Each one will have the suffix of the files in the array1 , to distinguish them.

That works like that I guess, but could you see a better way to practice? Because the nested loop is pretty uncomfortable (btw, the python script gives the good directories)

for i in ${array1[@]}; do
    for j in ${array2[@]}; do
        for l in ${array3[@]}; do
            python my_script.py $i $j $l
        done
    done
done

The contents of the arrays are these ones :

echo ${array1[@]}
flnc.bc1004.fq flnc.bc1005.fq flnc.bc1006.fq

echo ${array2[@]}
file_bc1004.gff file_bc1005.gff file_bc1006.gff 

echo ${array3[@]}
bc1004_stats.txt bc1005_stats.txt bc1006_stats.txt

If I run the script for the files corresponding to "bc1004", it looks like :

python my_script.py flnc.bc1004.fq file_bc1004.gff bc1004_stats.txt

And I should get an output directory like my_output_dir_bc1004

Best

Ivan
  • 6,188
  • 1
  • 16
  • 23
Paillou
  • 779
  • 7
  • 16
  • 1
    A nested loop multiplies by the number of elements in each array. Is that what you want? – Barmar Jan 05 '21 at 16:18
  • 1
    @Barmar I think OP is looking for [this](https://stackoverflow.com/questions/17403498/iterate-over-two-arrays-simultaneously-in-bash), but with three arrays. – oguz ismail Jan 05 '21 at 16:20
  • I suspect so, but it's not clear. He talks about 3 arguments, but also about running it 3 times. Which of these corresponds to the 3 arrays? – Barmar Jan 05 '21 at 16:32
  • 1
    Are there 3 elements in each array? If the arrays had 4 elements, would you run it 4 time? – Barmar Jan 05 '21 at 16:33
  • Can you show example contents of the 3 arrays, and what the 3 runs should be? – Barmar Jan 05 '21 at 16:45
  • To respond to "Are there 3 elements in each array? If the arrays had 4 elements, would you run it 4 time?" , the answer is yes. And for example, there would be 4 files in each arrays. – Paillou Jan 05 '21 at 17:07
  • 1
    Change `python` to `echo` to make sure that loops are working as expected. And it's a good practice to wrap array in "", like this `"${array1[@]}"` – Ivan Jan 05 '21 at 17:17
  • 1
    The topic "Iterate over two arrays simultaneously in bash" answers my question. Thanks a lot – Paillou Jan 05 '21 at 17:23

0 Answers0