Title: Bash Scripting-4- Using conditionals (if else in shell script)
1Bash Scripting-4- Using conditionals (if else in
shell script)
Posted by Prince DAN January 13, 2020 in DevOps
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. Lets
start with if-then statement,
2Using if then statement in bash script
Syntax If linux_command then some_other_linux_co
mmands fi As syntax shows, when the first
command mentioned after if works, then only the
command after then condition will work. Lets
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 userec2-user if grep user
/etc/passwd then echo user exists echo
contents of his home directory are ls -a
/home/user
3fi 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 el
se some_other_linux_commands fi Lets see an
example, !/bin/bash userec2-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
4fi The script is simple, first, it will look for
a user if the user exist, then it will show the
users home directory content, else will print
user does not exist. Now lets see another
example, where we will use multiple if
statements, !/bin/bash userec2-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 users 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. Lets have a look at an
example, !/bin/bash
5user1ec2-user user2root 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.
6If you think we have helped you or just want to
support us, please consider these- Connect to
us Facebook T witter Linkedin TheLinuxGURUS
are thankful for your continued support.
TheLinuxGURUS.com