In this article, we will explain how to use conditional statements in shell scripting. Along with why programmers use them often, and their benefits. So keep reading if you wish to know more.
A conditional statement is an essential construct of structured programming. Using shell scripting, your program can follow different execution paths depending on the value of certain variables or conditions in a program.
Introduction to shell programming/bash scripting
The shell scripts can be compared with a movie script in which the actors are instructed to act or say different things at different times of the movie. Similarly, bash scripts are a set of commands executed on a shell, aiming to automate various system-level tasks.
For instance, the following is a shell script to check if two numbers are equal:
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
How to Use Conditional Statements in Shell Scripting
There are three critical constructions of any structure programming language a) sequence, b) selection c) iteration. Typically, a program executes in sequence from top-to-bottom. But sometimes, you should run a set of statements conditionally.
If the condition is true, you may want to execute one set of statements. On the other hand, another set of statements is executed if the condition is false.
In such cases, selection or conditional statements are used. Finally, you may want to execute a set of statements repeatedly. During this, iteration statements are used. The objective of this article is to discuss conditional statements.
Conditional statements in shell scripting
There are five types of conditional statements in shell scripting
- if statement
- if-else statement
- if..elif..else..fi statement (Else If ladder)
- if..then..else..if..then..fi..fi..(Nested if)
- switch statement. We will explain each one of them in the following sections
In the following paragraphs, we will discuss these conditional statements in detail.
if statement
In this type of statement, we first specify the condition/expression in the if statement. If the condition is true, then the following ‘then’ block will be executed. The syntax of this statement can be specified as follows:
if [expression]
then
statement
fi
We have used the ‘if’ keyword and then specify a condition. The value of the condition may be true or false. If the condition is true, the statements following the keyword ‘then’ are executed immediately following the keyword’ then’. If the condition is false, the statements inside will not be executed.
The program will move to execute instructions specified after the keyword ‘fifi’.
Note: Like the C programming language.
if-else statement
We specify a condition with the if statement. If the condition is true, the corresponding block will be executed. Otherwise, the other block will be executed. The syntax of this type of statement is as follows:
if [expression]
then
statement1
else
statement2
fi
Tip: When writing conditions, it is advisable to indent your code. This will make your code pretty and improve your program’s readability and writability.
if..elif..else..fi statement (Else If ladder)
Here you specify conditions with if and every else if statements. If the condition is true, the corresponding block will be executed. However, if no condition is true, the last block, (i.e., else, will be executed).
The syntax of this type of conditional statement is as follows:
if [expression1]
then
statement1
statement2
.
.
elif [expression2]
then
statement3
statement4
.
.
else
statement5
fi
if..then..else..if..then..fi..fi..(Nested if)
In this type of conditional statement, you specify nested if else blocks. When the condition specified in the first if is true, it then enters into its block. Inside this block, there can be other if…else statements that are checked, and the corresponding block is executed.
The general syntax for this type of condition statement is as follows:
if [expression1]
then
statement1
statement2
.
else
if [expression2]
then
statement3
.
fi
fi
switch statement
In this type of statement, you specify an expression or variable in the switch. Then, a set of cases corresponds to various values of the specified expression. If the value matches any case, the block corresponding to the case is executed. The matching case block will execute all statements until a semicolon ‘;’ is found.
The general syntax of the switch statement is as follows:
case in
Pattern 1) Statement 1;;
Pattern n) Statement n;;
Esac
Specifying conditions/expressions in conditional statements
While specifying conditional statements, you can use various operators. For instance, && (AND) operator can combine two conditions, and the result will be true if both conditions are true.
The following are some of the other operators helpful with conditional statements:
-eq
– Equality check-ne
– Not equals-lt
– Less than-gt
– Greater than-le
– Less than equal to-ge
– Greater than equal to
Note: In shell scripting, you can use $0, $1, $2 … to access the various command line arguments passed to the program.
In this article, we have discussed conditional statements in shell scripting. They are an essential part of any programming language. Additionally, we even tackled the five types of conditional statements in shell scripting, along with their respective general syntax.
And lastly, we also talked about how various operators could be combined to write more powerful conditions in shell scripting. If you have any questions, feel free to drop a comment down below.
If this guide helped you, please share it.