Up until now, we have discussed using environment variables, using local & global variables & also about performing arithmetic operations in bash. Now we are ready to discuss some advanced stuff, we will discuss how to use if else in bash script & also other conditional statements.

Recommended Read: Examples on how to use RPM command in Linux

Also Read: Bash Scripting- 5 – Performing FILE, STRING comparisons & NUMERIC comparisons in Bash

If-then, else, elif are all called conditional statements. A conditional statement is used when we are required to satisfy a condition first, then execute a single or number of other commands. So we will be discussing all of these with the help of some examples. Let’s start with if-then statement,

Using if then statement in bash script

Syntax

If linux_command

then

              some_other_linux_commands

fi

As syntax shows, when the first command mentioned after ‘if’ works, then only the command after ‘then’ condition will work. Let’s see a simple script,

Example:

#!/bin/bash

If top

then

              echo “Script works”

fi

Here we have only use a single command, but we can use a number commands as well.

Another example,

#!/bin/bash

user=”ec2-user”

if grep $user /etc/passwd

then

              echo “user exists”

              echo “contents of his home directory are”

              ls -a /home/$user

fi

Here, we have created a variable for user & we are then checking if the user exists in ‘/etc/password’. If the user exists, then it will echo the same & will then display the contents of its home directory.

Using if else in shell script

Until now, we were presuming that the outcome of the condition will be positive but if that is not the case. So we use if then else statement to have an outcome for both conditions.

Syntax

If linux_command

then

              some_other_linux_commands

else

              some_other_linux_commands

fi

Let’s see an example,

#!/bin/bash

user=”ec2-user”

if grep $user /etc/passwd

then

              echo “user exists”

              echo “contents of his home directory are”

              ls -a /home/$user

else

              echo “User does not exist”

fi

The script is simple, first, it will look for a user & if the user exist, then it will show the user’s home directory content, else will print “user does not exist”. Now let’s see another example, where we will use multiple if statements,

#!/bin/bash

user=”ec2-user”

if grep $user /etc/passwd

then

              echo “user exists”

              echo “contents of his home directory are”

              ls -a /home/$user

else

              echo “User does not exist”

              if ls –a /home/$user

              then

                            echo “But the user has a directory”

              fi

fi

The script is the improvised version of the above-mentioed script, it will look for a user & if the user exists, then it will show the user’s home directory content, else will print “user does not exist” & then it will also look if there is a home directory for the mentioned user.

Using if-elif-else in bash script

Another method for using multiple if statements is by using elif command. Its especially useful, when you are writing long scripts when it might become difficult to keep track of conditional statements. Let’s have a look at an example,

#!/bin/bash

user1=”ec2-user”

user2=”root”

if grep $user1 /etc/passwd

then

              echo “user exists”

              echo “contents of his home directory are”

              ls -a /home/$user

elif

              if grep $user2 /etc/passwd

              then

                            echo “user exists”

                            echo “contents of his home directory are”

                            ls -a /home/$user

else

              echo “User does not exist”

fi

Here we have changed the above-mentioed script to look for two users with the help of elif statements. We can also use multiple elif statements like other statements.

All these were some very basic examples of using if else in shell script. We will be discussing some other examples in future tutorials. So we now end this tutorial on using if else in shell script, please feel free to send any questions, queries or suggestions using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter | Linkedin

TheLinuxGURUS are thankful for your continued support.