if - else statement
In this conditional statement, when the condition is true the if block is executed and when the condition is false the else block is executed.
Syntax
if [ expression ]
then
statement 1
else
statement 2
fi
Example
#!/bin/sh
a=20
b=20
if [ $a == $b ]
then
# If they are equal then print this
echo "a is equal to b"
else
#else print this
echo "a is not equal to b"
fi
Output
a is equal to b
if-elif-else statements
In this conditional statement, when the condition is true the if block is executed and when the if condition is false, the elif condition is checked and finally when the elif condition is also false, the else block is executed.
Syntax
if [ expression1 ]
then
statement1
elif [ expression2 ]
then
statement2
else
statement3
fi
Example
#!/bin/sh
a=15
b=20
if [ $a == $b ]
then
echo "a is equal to b"
elif [ $a -gt $b ]
then
echo "a is greater than b"
elif [ $a -lt $b ]
then
echo "a is less than b"
else
echo "None of the condition met"
fi
Output
a is less than b
Nested if-else statement
Inside the normal if-else statement more if-else statements are present.
Syntax:
if [ expression1 ]
then
statement1
statement2
.
else
if [ expression2 ]
then
statement3
.
fi
fi
Example:
#!/bin/sh
a = 10
b = 20
c = 5
if [$a -lt $b]
then
if [$a -lt $c]
then
echo “a is less than b and c”
else
echo “a is less than b and greater than c”
fi
else if [$a -gt $b]
then
echo “a is greater than b”
else
echo “a and b are equal.”
fi
Output
a is less than b and greater than c.
Switch statement
In the switch statement, the value of a variable is matched against predefined cases. If the specified value matches with any of the cases then it will execute the block of that particular case. When a match is found all of the associated statements until the double semicolon (;;) is executed. A case will be terminated when the last command is executed.
If there is no match, the exit status of the case is zero.
Syntax
case in
Pattern 1) Statement 1;;
Pattern n) Statement n;;
esac
Example
#!/bin/sh
company="Google"
#Pass the variable in string
case "$company" in
#case 1
"Google") echo "CTC: 33 Lac" ;;
#case 2
"Microsoft") echo "CTC: 52 Lac" ;;
#case 3
"Amazon") echo "CTC: 45 Lac" ;;
esac
Frequently Asked Questions
What is a shell script file?
A file containing one or more commands is known as a shell script. Shell scripts offer a simple way to execute hard tasks, lengthy or complex sequences of commands, and repetitive operations. The system runs the command sequence in a shell script file when its name is entered.
Why is shell scripting used?
Admins use them to get regular backups. Compared to other languages like C or C++, they are simpler to write and debug. The shell script can be copied to other UNIX and related operating systems and run there. Shell scripts are also employed for routine system monitoring.
Is shell scripting easy?
Shell script syntax is similar to other languages. It would be quite simple to start with it if you have prior knowledge of any programming language, such as Python, C/C++, etc.
Conclusion
Congratulations on finishing this article. In this article, we discussed conditional statements in shell scripting. We saw examples of if, if-else, if-elif-else and switch statements with their syntaxes.
Do not stop learning! We recommend you to read some of our articles -
🔥 Introduction to Linux
🔥 Linux Kernel
🔥 Linux Directories
🔥 Linux Commands list
Nmap commands
Head to the Guided Path on the Coding Ninjas Studio and upskill in Data Structures and Algorithms, Competitive Programming, System Design, and many more courses.
If you want to Practice top Coding Problems, attempt mock tests, or read interview experiences, head to the Coding Ninjas Studio, our practice platform.
We wish you Good Luck!🎈 Please upvote our blogs if you find them useful 🏆 to help other ninjas grow.