Conditional Statements in Bash Scripting

Conditional Statements in Bash Scripting

Conditional Statements in Bash Scripting

Today we will be taking a look at Conditional Statements in Bash Scripting.


What are Conditional Statements?

Conditional Statements are the statements in a programming language that are used to handle decisions. 
If this happens then do this, else do this. 
E.g - If you are 18+ or 18 then you can drive. But if you are not then you can't. To make the decisions like this we used Conditional Statements.

First, we will be using the "if" statement in Bash Scripting. 

As it's a single Conditional Statement it will be only able to check only one condition for us. 
But before we start checking for the conditions must know some operator that we are using to check the conditions. 


-eq --> Equal to

-ne --> Not equal to

-gt --> Greater than

-ge --> Greater than or equal to

-lt --> Less than

-le --> Less than or equal to


As you can see the above are the operators that we will be using while dealing with conditions. 

Syntax:- 


if[Condition]
then
Statement
fi


The fi indicates the ending of the if statement. 

Example:- Writing a simple program to check whether the user's Entered number is 5 or not 


#! /bin/bash

read -p "Enter a random number between 1 - 10" number

if [ $number -eq 5 ]
then
echo "You enter a correct number which is 5"
fi


This program is asking the user to enter a random number between 1 to 10. Then store it in a variable, then it's checking whether the variable is equal to 5 if it's true, the block is executed. 


┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter a random number between 1 - 10:- 1



┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter a random number between 1 - 10:- 11



┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter a random number between 1 - 10:- 5
You enter a correct number which is 5


As you can see above when the user entered 5, the block gets executed and it prints the statement given to it.

This was an example of a very simple question where we used a single if condition. as you saw, it was outputting nothing when the condition was not met, for this case we have an "else" block as it will get executed when the if statement is not met. And in the above example, we have used square brackets and -eq for condition but if you want to use symbols then you have to use double parenthesis for that. 


#! /bin/bash

read -p "Enter a random number between 1 - 10:- " number


if(($number == 5))
then
echo "You enter a correct number which is 5"
else
echo "The number is not as required."
fi



┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter a random number between 1 - 10:- 6
The number is not as required.


By just using the if & else you can make so many programs of your choice like finding even numbers, finding the odd numbers. 

Conditional Statements in Bash Scripting


Example:- Finding whether the number is even or odd. 

Code:- 


#! /bin/bash

read -p "Enter a random number between 1 - 10:- " number


if(($number%2 == 0))
then
echo "$number is even"
else
echo "$number is odd"
fi


Output:- 


┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter a random number between 1 - 10:- 5
You enter a correct number which is 5



┌──(kali㉿kali)
Enter a random number between 1 - 10:- 84
The number is not as required.



┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter a random number between 1 - 10:- 8390
The number is not as required.


But this is only to check one condition when the condition is true the if executes and when the condition is false then the else get executed, but what if you want some more condition checked, for that we have elif. 

Code:- 


#! /bin/bash

read -p "Enter a number:- " number


if(($number == 5))
then
echo "Number is 5"

elif(($number == 10))
then
echo "Number is 10"

else
echo "Try number divisible by 5 between 1 - 10 "
fi



Output:- 


┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter a number:- 13
Try number divisible by 5 between 1 - 10



┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter a number:- 5
Number is 5


┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter a number:- 10
Number is 10

Conclusion:- 

We have successfully used the conditional statements in Bash Scripting, you can search for programs based on if-else and try to practice them in Bash Scripting. 

Stay Tuned ✌✌

Next Post Previous Post
No Comment
Add Comment
comment url