GNU parallel: while loop with sequential commands

I need to run a bash script on a list of subjects. I would like to build a while loop, that runs on multiple subjects at the same time. However, the commands within the loop must run sequentially.

May you please confirm me that a sintax like this would do the job?

file="subjects.txt"
foo () {
command $subj etc
command 2 $subj etc
command 3 $subj etc
}
while read subj; do foo "$subj" &
done <$file

Thank you in advance for your help.

Ramtin

1 Answer

file="subjects.txt"
foo () { subj="$1" command $subj etc command 2 $subj etc command 3 $subj etc
}
export -f foo
parallel foo :::: "$file"

It will run n jobs in parallel (where n = number of CPU threads). To change that use parallel -j10 for 10 jobs in parallel.

Read chapter 1+2 of (can be downloaded at ). Your command line will love you for it.

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like