Skip main navigation

New offer! Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. New subscribers only. T&Cs apply

Find out more

If and If Else Statements

use of conditional expressions in conditional statements

In the previous step, we looked at conditional expressions, the means by which we can perform comparisons between values of different types. Now, we’re going to make use of those expressions in conditional statements.

IF statements

Conditional statements come in many forms. The most basic form essentially says: IF our conditions are met, THEN execute the following code.

We can write our if statements in several ways:

if [[ condition ]]
then
command
fi

So, if the expression within the square brackets returns true, then the code found between then and fi will be executed. If the expression returns false, the script will ignore (i.e. not execute) any code which lies between then and fi. Notice that we end our if statement with fi which is if spelt backwards.

An alternative format that you might come across uses a ‘;’ to allow then to be on the same line as the conditional expression:

if [[ condition ]] ; then
command
fi

You’ll notice that in these examples, we’ve used spacing to indicate the code which will run if the conditional expression returned true. This is known as indenting and, although there are no requirements for it in Bash, it is a good coding practice to follow for clean, readable code.

IF statements with AND/OR logic

We can use two (or more) conditional expressions with our if statement using the AND and/or OR conditions.

For example, let’s say that we have a file and we want to check that it exists, is readable and that it isn’t empty. If our file meets these criteria, we want to print “File is good” and if not, print “File is bad”.

First, let’s write our script:

#!/usr/bin/env bash

# Set the path for our file
file="file.txt"

# Check whether file exists, is readable and has data
if [[ -e ${file} ]] && [[ -r ${file} ]] && [[ -s ${file} ]]
then
# Execute this code if file meets those conditions
echo "File is good"
else
# Execute this code if file does not meet those conditions
echo "File is bad"
fi

Now, let’s run our script knowing that our file can’t meet the criteria as it doesn’t exist:

./script.sh
File is bad

Next, let’s create an empty file and try running our script again:

touch file.txt
./script.sh

Again, our script returns “File is bad” as it hasn’t met all of our conditions. Finally, let’s add some data to our file and try again:

echo "hi" > file.txt
./script.sh

Bingo! We have met all of the conditions and now our “File is good”.

IF..ELSE statements

We can extend our conditional statement to have another clause by using an if..else statement. Here we are saying, IF our conditions are met, THEN execute the following commands. However, ELSE IF these conditions are not met, execute a different set of commands.

The syntax for this looks like:

if [[ condition ]]
then
command1
else
command2
fi

IF..ELIF..ELSE statements

Sometimes, you may need to test more than one statement. The syntax for this looks like:

if [[ condition1 ]]
then
command1
elif [[ condition2 ]]
then
command2
else
command3
fi

It’s worth mentioning that you can have more than one elif clause in your if..elif..else statement. But, as we will soon discuss, there are more efficient ways of building that type of conditional statement.

Your task:

Write a Bash script called temperature.sh that:

  1. read in a command line argument into a variable called temperature
  2. has a variable called min and give it a variable of 10
  3. has a variable called max and give it a variable of 30
  4. returns “Too hot!” if temperature is greater than max_temperature
  5. returns “Too cold!” if temperature is less than min_temperature
  6. returns “Just right!” if temperature is greater than or equal to the min_temperature and less than or equal to the max_temperature

Test your Bash script with the following command:

./temperature.sh 22

What does your script output?

Please try to answer the questions yourself first and then compare the results with other learners. Finally, you can find solutions in the download area.
© Wellcome Genome Campus Advanced Courses and Scientific Conferences
This article is from the free online

Bioinformatics for Biologists: An Introduction to Linux, Bash Scripting, and R

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now