Control flow

Authors

Marie-Hélène Burle

Alex Razoumov

By default, scripts get executed linearly from top to bottom. Often however, you want to control what gets executed when.

This section covers various ways to control the flow of execution through a script.

For this section, we will play with files created by The Carpentries.

You can download them into a zip file called data.zip with:

curl --output data.zip https://mint.westdri.ca/bash/data.zip

You can then unzip that file with:

unzip data.zip

You should now have a data directory.

cd into it:

cd data

Conditional executions

Sections of a script can be executed or not depending on some conditions. To achieve this, we first need to have expressions that define these conditions.

Predicates

Predicates are expressions that, when evaluated, return an exit status of 0 if they are true and an exit status of 1 if they are false.

Here are examples of predicates:

[ $var == 'text' ] checks whether var is equal to 'text'.

[ $var == number ] checks whether var is equal to number.

[ -e name ] checks whether name exists.

[ -d name ] checks whether name is a directory.

[ -f name ] checks whether name is a file.

Make sure to have spaces around each bracket.

Your turn:

Let’s create a directory and a file:

mkdir d1
touch f1
mkdir: cannot create directory ‘d1’: File exists

Write the predicates that test whether:

  • d1 exists,
  • d1 is a file,
  • d1 is a directory,
  • f1 is a file,
  • f1 is a directory.

Success/failure of previous command

In its simplest form, conditional execution can be limited to the failure or success of the previous command.

xxxIn its simplest form, conditional execution can be limited to the failure or success of the previous command.

Predicates are expressions that return an exit status of 0 when they are evaluated if they are true and an exit status of 1 if they are false.

Conditional on success

Commands can be limited to running only if the previous command ran successfully thanks to &&.

Example:

Look at the following commands:

unzip bash.zip
rm bash.zip

This is equivalent to:

unzip bash.zip;
rm bash.zip

and to:

unzip bash.zip; rm bash.zip

This is what we did to get the data for the past few sessions.

In both cases, both commands will try to run. Now, if for some reason, the unzipping fails, we have deleted the zip file and we have to re-download it. Not a big deal here, but in some situations, executing a command if the one before fails can be a real bummer.

To prevent this, we can use the double-ampersand (&&) operator, which plays the role of a logical AND statement:

unzip bash.zip &&
rm bash.zip

This is equivalent to:

unzip bash.zip && rm bash.zip

If the unzipping works (if it returns a zero exit status), then the Zip file gets deleted. If however, the unzipping fails (if it returns a non-zero exit status), the script aborts and we haven’t lost our Zip file.

xxxxx return 0xxxx

returns truexxx

Conditional on failure

The opposite of && is || which plays the role of a logical OR statement: the following command only gets executed if the first one fails.

Example:

[ -e f1 ] || echo f1 does not exist
[ -e f2 ] || echo f2 does not exist
f2 does not exist

If statements

Sections of scripts can be executed (or not) based on conditions thanks to if statements.

Syntax

In its simplest form, if statements look like:

if [ predicate ]
then
    command1
    command2
    ...
fi

This can also be written as:

if [ predicate ]; then command1; command2; ...; fi

If the condition is true, the commands are executed, if the condition is false, nothing happens.

If you want a different set of commands to be executed when the condition is false, you add an else statement:

if [ predicate ]
then
    command1
    command2
    ...
else
    command3
    command4
    ...
fi

Of course, you can have multiple conditions defining trees of if statements. In that case, you use elif (any number of times):

if [ predicate1 ]
then
    command1
    command2
    ...
elif [ predicate2 ]
then
    command3
    command4
    ...
else
    command5
    command6
    ...
fi

Examples

var=f1

if [ -e $var ]
then
    echo $var does not exist
else
    echo $var does exist
fi
f1 does not exist
var=f2

if [ -e $var ]
then
    echo $var does not exist
else
    echo $var does exist
fi
f2 does exist

Conditionally repeated executions

Sections of scripts can be repeated as long as a condition returns True thanks to while loops.

Syntax

The syntax of a while loop in Bash is:

while [ predicate ]
do
    command1
    command2
    ...
done

The set of commands in the body of the while loop are executed as long as the predicate returns true.

Be careful that while loop can lead to infinite loops. Such loops need to be manually interrupted (by pressing <Ctrl+C>).

Example of infinite loop:

while true
do
    echo "Press <Ctrl+C> to stop"
    sleep 1
done

Repeated executions

Sections of scripts can be repeated for each element of a list thanks to for loops.

Collections

For loops run a set of commands for each item of a collection. How do you create those collections?

Listing items one by one

The least efficient method is to list all the items one by one:

Example:

for i in file1 file2 file3
do
    echo $i
done
file1
file2
file3

Wildcards

The molecules directory contains the following .pdb files:

ls *.pdb
cubane.pdb  ethane.pdb  methane.pdb  octane.pdb  pentane.pdb  propane.pdb

We want to rename these files by prepending “gas_” to their current names.

Wildcards don’t work here:

mv *.pdb gas_*.pdb
mv: target 'gas_propane.pdb': Not a directory

The solution is to use a for loop:

for file in *.pdb
do
    mv $file gas_$file
done

This can also be written as a one-liner, although it is harder to read:

for file in *.pdb; do mv $file gas_$file; done

Brace expansion

Collections can also be created with brace expansion.

Examples:

echo {1,2,5}
1 2 5

Make sure not to add a space after the commas.

echo {list,of,strings}
list of strings
echo {file1,file2}.sh
file1.sh file2.sh
ls -l {ethane,methane,pentane}.pdb
ls: cannot access 'ethane.pdb': No such file or directory
ls: cannot access 'methane.pdb': No such file or directory
ls: cannot access 'pentane.pdb': No such file or directory
echo {1..5}
1 2 3 4 5
echo {01..10}
01 02 03 04 05 06 07 08 09 10
echo {r..v}
r s t u v
echo {v..r}
v u t s r
echo {a..e}{1..3}
a1 a2 a3 b1 b2 b3 c1 c2 c3 d1 d2 d3 e1 e2 e3
echo {a..c}{a..c}
aa ab ac ba bb bc ca cb cc
echo {1..5}.txt
1.txt 2.txt 3.txt 4.txt 5.txt
echo file{3..6}.sh
file3.sh file4.sh file5.sh file6.sh

Brace expansion can be used to create lists iterated over in loops, but also to apply commands to files or directories.

Sequences

Collections can also be sequences:

seq 1 2 10
1
3
5
7
9

Here, 1 is the start of the sequence, 10 is the end, and 2 is the step.

Such a sequence could be used in a loop this way:

for i in $(seq 1 2 10)
do
    echo file$i.txt
done
file1.txt
file3.txt
file5.txt
file7.txt
file9.txt

Syntax

The general structure of a for loop is as follows:

for <iterable> in <collection>
do
    <command1>
    <command2>
    ...
done

Your turn:

In a directory the command ls returns:

fructose.dat  glucose.dat  sucrose.dat  maltose.txt

What would be the output of the following loop?

for datafile in *.dat
do
  cat $datafile >> sugar.dat
done
  1. All of the text from fructose.dat, glucose.dat and sucrose.dat would be concatenated and saved to a file called sugar.dat.

  2. The text from sucrose.dat will be saved to a file called sugar.dat.

  3. All of the text from fructose.dat, glucose.dat, sucrose.dat, and maltose.txt would be concatenated and saved to a file called sugar.dat.

  4. All of the text from fructose.dat, glucose.dat and sucrose.dat will be printed to the screen and saved into a file called sugar.dat.

Here is a video of a previous version of this workshop.