Home Linux Bash Scripting – If, Else If, Else Tutorial

Bash Scripting – If, Else If, Else Tutorial

by admin
else if bash

This short tutorial shows how to use if else statements in Bash. An if else statement in programming is a conditional statement that runs a set of statements depending on whether an expression is true or false. This ‘decision making’ capability can be very useful when used in Bash shell scripts, as with any other programming language. In a bash script you will typically see a number of ways in which IF statements are used. These are:

  • IF statements
  • IF, ELSE statements
  • IF, ELIF, ELSE statements

Let’s take a look at some practical examples of how IF statements can be used in bash scripting.

Bash IF Statement

To start with, we will give an example of a simple IF statement. The scenario used here is to use the test command with an IF statement to check if a file (app.log) exists.

In Bash, you can use the test command to check whether a file exists.

#! /bin/bash
LOG=app.log
if test -f "$LOG"; then
    echo "$LOG exists."
fi

As the file exists in the directory where I ran this bash script, it returned the following:

app.log exists.

The downside of this is that if the file didn’t exist, the script wouldn’t return any value. This is where if else comes in.

Bash IF Else Statement

To make the script above a little more useful, it would be better if, in the event of the file not existing, it returned a statement to say that the file is missing. This can be accomplished by adding an else statement to the bash script:

#! /bin/bash
LOG=app.log
if test -f "$LOG"; then
    echo "$LOG exists."
else
    echo "$LOG is missing."
fi

With the else statement added, if the file is not found, the following will be returned:

app.log is missing.

If the test -f "$LOG" evaluates to True, the first echo statement will be executed. Otherwise, if test -f "$LOG" returns False, the second echo statement (file missing!) will be executed.

Note: You can only have one else in a statement.

This script could easily now be changed to perform a more useful action if a specified file exists. For example, we could have the script delete a file if it exists, or maybe move or zip the file for archiving purposes.

Bash Else IF Statement

We can continue to add logic to the script by including an else if statement. Let’s take a look at an example:

#! /bin/bash
LOG=app.log
LOG2=app-1.log

if test -f "$LOG"; then
    echo "$LOG exists."

elif test -f "$LOG2"; then
        echo "$LOG2 exists."

else
    echo "No logs are present"
fi

If the test -f "$LOG" evaluates to True, the first echo statement will be executed. If it is False then the ELIF (else if) statement will be evaluated. If this evaluates to True then the second echo statement will be executed. If none of the statements evaluate to True, then the last echo statement is executed.

It’s fine to have more than one elif clauses in the statement, whilst the else clause is optional. Each condition (IF, ELIF) is evaluated sequentially. It’s important to be aware that once a condition evaluates as True, the remaining conditions are not evaluated.

A more practical example of how this can be used is in a script that checks a systems CPU usage, and returns a message based on the CPU percentage value:

CPU=`mpstat | grep -A 5 "%idle" | tail -n 1 | awk -F " " '{print 100 -  $ 12}'a | cut -c1-2`

if [[ $CPU -gt 95 ]]
then
  echo "ALERT: CPU Usage is greater than 95%."
elif [[ $CPU -gt 90 ]]
then
  echo "WARNING: CPU Usage is greater then 90%."
else
  echo "CPU Usage is normal."
fi

In the example above, the script is retrieving the current CPU usage using mpstat, then we are evaluating the returned value and displaying an appropriate message.

Multiple Conditions with ELSE IF Statements

Multiple conditions can be evaluated in the same statement by using the logical AND operator. Let’s take a look at an example:

i=8
if [ "$i" -ne 6 ] && [ "$i" -ne 9 ]; then
   echo 'i is neither 6 nor 8'
else
   echo "i is $i"
fi

In this example the AND operator && is used to check if i (9) is not equal to both 6 and 8. If both conditons are met then the i is neither 6 nor 8 message will be displayed. Otherwise the else statement will be evaluated.

Final Thoughts

In this tutorial you have seen how IF, IF ELSE and IF, ELIF, ELSE statements can be used in Bash scripts to introduce logic and decision making by evaluating specified conditions and responding accordingly.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More