Loops - GarzaLab/Documentation GitHub Wiki

The simplest loop you can do in bash scripts or in the terminal. Remember that in scripts you don't need to use ;, just push ENTER. To write the loop in the terminal you do need ; for every break.

To run the loop in terminal just click ENTER. To create a script use textedit in Ubuntu or nano in the terminal. Save the script with the suffix sh as in script.sh. To run the script, use terminal and write $> bash script.sh.

for {variable} in {variable value};

do

.
.
.

done

Here is an example. This loop with show the names of each Nifti file inside the folder:

Script

#!/bin/bash

for file in *.nii.gz;

do

echo ${file}

done

or

Terminal

for file in *.nii.gz; do echo ${file}; done

To use variable values inside a text file, such as a list of subjects (i.e. AD_005, HC_010), you can do a text file with one column and many rows, each with the value. Then:

Script

#!/bin/bash

cat list.txt | while read file;

do 

echo ${file}

done

or

Terminal

cat list.txt | while read file; do echo ${file}; done

--

Eduardo