BASH Scripting – Conditional Statements (if-then, else, elif)

BASH Scripting – Conditional Statements (if-then, else, elif)

In this tutorial, we will be discussing some of the conditional statements (aka structured commands).

Firstly, what are Conditional statements ?

In a BASH scripts, commands run sequentially that means first line will be executed first, then next one & so on …… But there might arise a requirement when you need to execute a command only when a condition has been fulfilled. That’s where conditional commands come onto play.

So, in this tutorial we will be discussing some conditional commands. Let’s get going….

If-Then statement

This is the most basic type of conditional (structured) command. Syntax for using it is:

If command
then
Commands
fi

Let’s use it in an example

#!/bin/bash
#Script to see if statement in use
if date
then
echo “Command works”
fi

So, this seems simple enough, here when only date command runs then only we will get output i.e. “Command works”

Also, remember that we can use a number of commands in “then” statement.

#!/bin/bash
#Script to see multiple commands in use in “then ” statement
user= dan
if grep $user /etc/passwd
then
echo “user exists”
echo “& contents of his home directory are”
ls –a /home/$user
fi

These are some simple scripts where our output is provided when conditions are TRUE. So, what to do when our conditions are not TRUE, next conditional statement is the answer…


If-then-else Statement

For If-then statement, we could only provide a condition for when a command is fulfilled but what to do when our condition is not fulfiled. For that very purpose we use , if-then-else

Let’s see an example script:

#!/bin/bash
#Script to see if-then-else statement in action
if date
then
echo “Command works”
else
echo “Command is not right”
fi

here you can write something else in place of “date” & you will see that output for the script will be “Command is not right”

Let’s check out another example

#!/bin/bash
#Script to see multiple commands in use in then statement & else statement
user= dan
if grep $user /etc/passwd
then
echo “user exists”
echo “& contents of his home directory are”
ls –a /home/$user
else
echo “No such user found in the system”
fi

So, using  if-then-else makes our script provide an output in both cases i.e. when our conditions are met & when they are not.


Multiple if statements

There might be need to check several conditions in our script. So to achieve that we can use multiple if statement to check all our conditions:

#!/bin/bash
#Script to see use of multiple if statements
user= dan
if grep $user /etc/passwd
then
echo “user exists”
echo “& contents of his home directory are”
ls –a /home/$user
else
echo “No such user found in the system”
if ls –a /home/$user
then
echo “But user has a directory”
fi
fi

Above scripts looks for a user in system & if its exists then it will provide a positive response with its home directory’s contents. But when user doesn’t exists , it will look to see if it has a home directory available & will provide a directory.


Using ELIF statement

Although using multiple Ifs will get the job done but they might be hard to keep track in longer scripts & we might end up messing our scripts.

So, we use elif statement which will not only shorten our script but will also make our scripts look clean

#!/bin/bash
#Script to see use of multiple if statements
user= dan
if grep $user /etc/passwd
then
echo “user exists”
echo “& contents of his home directory are”
ls –a /home/$user
elif ls –a /home/$user
echo “No such user found in the system”
echo “But user has a directory”
fi

Also, we can also use else statemen here to print an output in case neither a user exists nor a directory.

#!/bin/bash
#Script to see use of multiple if statements
user= dan
if grep $user /etc/passwd
then
echo “user exists”
echo “& contents of his home directory are”
ls –a /home/$user
elif ls –a /home/$user
echo “No such user found in the system”
echo “But user has a directory”
else
echo “Neither a user exists nor a home directory”
fi

Also, remember we can use multiple elif statements  is your scripts.

This completes our tutorials on conditional statements but these are not all of it. In our coming tutorials we will discuss some more conditional statements.


whoami
Stefan Pejcic
Join the discussion

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.