How does one Parallelize a shell script with arguments using GNU parallel?

I am new to bash scripting. I have a shell script that runs several functions for longitudinal image processing in Matlab through terminal. I would like to parallelize the process in the terminal.

Here is a brief example of how it runs:

./script.sh *.nii -surface -m /Applications/MATLAB_R2018b.app/bin/matlab

*.nii refers to images from a single subject taken at different times (i.e. subj1img1 subj1img2 subj3img3). There are 3 images per subject in my case. So in each run, the script runs through all images of a single subject.

I would like to parallelize this process so that I can run this script for multiple subjects at the same time. Reading through GNU parallel with my little experience I wasn't able to figure out the code I need to write to make it happen. I'd really appreciate if anyone has any suggestions.

Upvotes: 1

Views: 129

Answers (2)

Ole Tange
Ole Tange

Reputation: 33685

parallel ./script.sh {} -surface -m /Applications/MATLAB_R2018b.app/bin/matlab ::: *.nii 

Upvotes: 1

nullPointer
nullPointer

Reputation: 4574

you can start them in the background using & in a for loop as below :

for f in *.nii
do
  ./script.sh "$f" -surface -m /Applications/MATLAB_R2018b.app/bin/matlab &
done

Upvotes: 0

Related Questions