Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run bash for loop and using GNU parallel?

I have a bash loop where I am passing variables to a script. I want to run these in parallel with GNU parallel

for FILE_NAME in FILE1 FILE2 FILE3; 
do
    ./SCRIPT -n $FILE_NAME
done

where I want the scripts to run in parallel as follows:

     ./SCRIPT -n FILE1
     ./SCRIPT -n FILE2
     ./SCRIPT -n FILE3

I am trying to use the GNU parallel command because it has been suggested a lot on here, but I am confused about where to put the parallel command if I am passing a variable to the script.

I have tried turning the FILE1 FILE2 FILE3 into a list:

parallel -a $FILE_LIST ./SCRIPT -n $FILE_NAME
for FILE_NAME in FILE1 FILE2 FILE3; 
do
    parallel ./SCRIPT -n $FILE_NAME
done

Do you have any suggestions?

like image 382
Maria Avatar asked Oct 27 '25 08:10

Maria


1 Answers

Try like this:

parallel ./SCRIPT -n {} ::: FILE1 FILE2 FILE3

Or, more succinctly if your files are really named like that:

parallel ./SCRIPT -n {} ::: FILE*
like image 76
Mark Setchell Avatar answered Oct 28 '25 20: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!