Bash Scripting-4- Using conditionals (if else in shell script) — The Linux GURUS
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
some_other_linux_commands
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:
Here we have only use a single command, but we can use a number commands as well.
Another example,
echo “contents of his home directory are”
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
some_other_linux_commands
some_other_linux_commands
Let’s see an example,
echo “contents of his home directory are”
echo “User does not exist”
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,
echo “contents of his home directory are”
echo “User does not exist”
echo “But the user has a directory”
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,
if grep $user1 /etc/passwd
echo “contents of his home directory are”
if grep $user2 /etc/passwd
echo “contents of his home directory are”
echo “User does not exist”
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:-
TheLinuxGURUS are thankful for your continued support.
Originally published at https://thelinuxgurus.com on January 13, 2020.