Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

increment var in gnu parallel

How can I use an increment variable within parallel? Note the $int variable in the output filename (prefix). I realize order can change, and that is fine, but what's useful is to have integers prefixing the output for downstream work (the length of ${array[@]} changes).

I'm trying to convert this for-loop into a nice parallel cmd to run on a cluster. One feature I really like is to have this process quit if any of the jobs have a non-zero exit status described here: https://www.gnu.org/software/parallel/parallel_tutorial.html#Termination

From:

var='infile.txt'
int=1
for file in ${array[@]}; do
  script.rb -i $var -o $int_$file
  ((int+=1))
done

To:

parallel --halt soon,fail=1 --jobs $NSLOTS script.sh -i $var -o int_{} ::: ${array[@]}

I was thinking something like a {1..array_len} construct might work to act as a counter but this doesn't: parallel --halt soon,fail=1 --jobs $NSLOTS script.sh -i $var -o {2}_{1} :::: ${array[@]} {1..${#array[@]}}

like image 605
lcb Avatar asked Oct 27 '25 06:10

lcb


1 Answers

I think you want this:

parallel --halt soon,fail=1 script.rb -i $var -o {#}_{} ::: ${array[@]}

where {#} is the job sequence number.


Here is a simple example:

parallel -k echo "param="{} ",sequence="{#} ::: 2 4 6 8

param=2 ,sequence=1
param=4 ,sequence=2
param=6 ,sequence=3
param=8 ,sequence=4
like image 78
Mark Setchell Avatar answered Oct 29 '25 23:10

Mark Setchell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!