# 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 commandfi

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 commandfi

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 filefile="file.txt" # Check whether file exists, is readable and has dataif [[ -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.shFile 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 command1else command2fi

## IF..ELIF..ELSE statements

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

if [[ condition1 ]]then command1elif [[ condition2 ]]then command2else command3fi

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.

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