1

I am new to the group so please apologize for any mistake or badly formulated question. Here's my problem. I have a few arrays of variables. I want to simultaneously start different jobs with all possible combinations (e.g. different tasks). What I don't know how to do, is to sequentially access all possible combination of variables.

Here's a test bash script which only prints out the set of variables at each iteration. This does not look good to me as it prints much more than I expected. As I have 3 arrays of 2,3 and 4 variables, I'd need a total of 2 * 3 * 4 = 24 tasks. Can someone explain me what I am doing wrong or if there's a better yet cleaner way of doing it?

Here's a little example:

#!/bin/bash
#$ -N combi_test
#$ -t 1-24

ACQ=('ei') VAR=('sh' 'rbf') TRAIN=(1000 2000 3000) ANGLES=(10 20 30 40)

for i in "${!ANGLES[@]}"; do for j in "${!VAR[@]}"; do for k in "${!TRAIN[@]}"; do echo "IS_${ACQ}tr${TRAIN[k]}${ANGLES[i]}_${VAR[j]}.h5" done done done

Thanks a lot in advance!

Ole Tange
  • 35,514
nik93
  • 11
  • 1
    The code prints exactly 24 lines for me. The only "issue" is ANGLE is not ANGLES. – Kamil Maciorowski May 05 '21 at 08:41
  • @KamilMaciorowski you are right, I found the problem. Now it also print 24 lines as well. However, this is done sequentially. Actually, what I'd like to do, is to simultaneously start many jobs each of which prints a line with a different combination of variables. Rather than printing all the lines in a sequential fashion. Would this be possible? – nik93 May 05 '21 at 08:57
  • There's a few questions on the site about launching stuff in parallel (just need to find the good ones...), see e.g. https://unix.stackexchange.com/questions/103920/parallelize-a-bash-for-loop https://unix.stackexchange.com/questions/35416/four-tasks-in-parallel-how-do-i-do-that https://unix.stackexchange.com/questions/211976/how-to-run-x-instances-of-a-script-parallel Basically it boils down to a) run them in the background, or better b) use GNU parallel or a version of xargs that can do it – ilkkachu May 05 '21 at 09:01
  • 1
    Here, with the loops, if you only care about the values and not the indices of the array items, you could loop over just the values with for angle in "${ANGLES[@]}"; do etc. (i.e., without the !) and then use ${angle} and not ${ANGLES[i]}. Which ever feels better, of course. – ilkkachu May 05 '21 at 09:04

1 Answers1

0

Try:

parallel echo "IS_${ACQ}_tr{3}_{1}_{2}.h5" ::: "${ANGLES[@]}" ::: "${VAR[@]}" ::: "${TRAIN[@]}"
Ole Tange
  • 35,514