In this topic, we'll understand the way to use if statements in Bash scripts to urge our automated tasks completedBash if statements are beneficial. they're wont to perform conditional tasks within the sequential flow of execution of statements. If statements usually allow us to form decisions in our Bash scripts. they assist us to make a decision whether or to not run a bit of code based upon the condition that we may set.
Basic if Statements
A basic if statement commands that if a specific condition is true, then only execute a given set of actions. If it's not true, then don't execute those actions. If the statement is predicated on the subsequent format:
Syntax
if [ expression ];
then
statements
The statement between then and fi (If backwards) are going to be executed as long as the between the square brackets) is true.
Note: Observe the spaces utilized in the primary line and a semicolon at the top of the primary line; both are mandatory to use. If conditional statement ends with fi.
We can use multiple if condition using and/or or both in our condition.
Example 1: with && in our script
if [ expression_1 ] && [ expression_2 ];
then
statements
fi
example 2 using || in our script
if [ expression_1 ] || [ expression_2 ];
then
statements
fi
example 3 using both and & or in our script.
if [ expression_1 && expression_2 || expression_3 ];
then
statements
fi
Here is the simple use case of if in bash,
#!/bin/bash
read -p " Enter number : " number
if [ $number -gt 100 ]
then
echo "Value is greater than 100"
fi
Output pf this code depends on our input, so in the following picture you will se how our output could vary with our input.
This code didn’t gave any output when the input was lesser than 100, and when the input was 450 it gave the output.
Example 2: In this example we are comparing two strings and we will see how it works, lets see in the code.
#!/bin/bash
# if condition is true
if [ "mylife" == "mylife" ];
then
echo "true condition"
fi
# if condition is false
if [ "myscript" == "yourscript" ];
then
echo "false condition"
fi
Output
Here you can see the that we are only getting one output because another condition is false, and 1st condition is true.
Example 3: In this example we are using various if condition too see the workflow,
!/bin/bash
#if condition (greater than) is true
if [ 10 -gt 3 ];
then
echo "10 is greater than 3."
fi
#if condition (greater than) is false
if [ 3 -gt 10 ];
then
echo "3 is not greater than 10."
fi
#if condition (lesser than) is true
if [ 3 -lt 10 ];
then
echo "3 is less than 10."
fi
#if condition (lesser than) is false
if [ 10 -lt 3 ];
then
echo "10 is not less than 3."
fi
#if condition (equal to) is true
if [ 10 -eq 10 ];
then
echo "10 is equal to 10."
fi
#if condition (equal to) is false
if [ 10 -eq 9 ];
then
echo "10 is not equal to 9"
fi
Output of the code:
Nested if:
We can also nested if condition in bash shell script, nested means we can write a code in which we can check that if one condition is true and also check if the condition with in the condition is true or false, better if we see in code then you will understand better.
#!/bin/bash
#Nested if statement
a=3
if [ "$a" -gt 0 ]
then
if [ "$a" -lt 5 ]
then
echo "The value of \"a\" lies somewhere between 0 and 5."
fi
fi
# Same result as:
if [ "$a" -gt 0 ] && [ "$a" -lt 5 ]
then
echo "The value of \"a\" lies somewhere between 0 and 5."
fi
Here is the code in which we have used two if condition to check the same statement.
Following the output for the above code.