Using bash to loop on subjects

Most of our tutorials give you examples of the command-line to execute scripts on a single subject. They also give you the option of downloading the bash script associated to the tutorial.

Once you have mastered all processing steps to run a start-to-finish pipeline for a subject (see for instance From raw diffusion to DTI, fODF, and tractogram), you might want to start running pipelines on many subjects automatically without having to enter the command lines manually again for each new subject. The simplest way to do that is to loop on all subjects in a bash script, which is what we will show here.

for subj in "subj1 subj2"
do
    # Run all steps!
    ...
done

If your database is well organized and looking like this:

├── root_folder
│   ├── subj1
│   ├── subj2

You can do:

for subj_folder in $root_folder/*
do
    subj_name=${subj_folder#$root_folder}
    echo "Processing subject $subj_name!"

    # Run all steps!
    ...
done

Note however that this will still run all commands one after the other, which prevents scalability to large cohorts. For more computationally efficient ways to run pipelines, see use_nextflow.