If Statement:
We use the "if statement" for checking the condition. If we have multiple conditions we can also use multiple "if statements".
Syntax:
if [ expression ]
then
statement
fi
Example For If Statement:
#!/bin/bash
a=5
b=30
if [ $a -lt $b ]
then
echo "a is less than b"
fi
if - else statement
Syntax
if [ expression ]
then
statement1
else
statement2
fi
Example For If -else Statement:
#!/bin/bash
a=5
b=30
if [ $a -lt $b ]
then
echo "a is less than b"
else
exho "b is less than a"
fi
Multiple If-Else Statement
Syntax
if [ expression1 ]
then
statement1
statement2
.
.
elif [ expression2 ]
then
statement3
statement4
.
eg.Syntax .
else
statement5
fi
Example For Multiple If -else Statement:
#!/bin/bash
echo "enter the color"
read color
if [ ${color} == "Red" ]
then
echo "You have enter Red Color"
elif [ ${color} == "Blue" ]
then
echo "You have enter Blue Color"
else
echo "Color is Neither the blue one nor the red one"
fi
Nested if-Else statement:
Syntax:
if [ expression1 ]
then
statement1
statement2
.
else
if [ expression2 ]
then
statement3
.
fi
fi
Example For Nested If -else Statement:
#!/bin/bash
echo "enter the color"
read color
echo "enter the car name"
read car
if [ ${color} == "Red" ]
then
echo "You have selected red color"
if [ ${car} == "BMW" ]
then
echo " You have selected Red BMW"
fi
elif [ ${color} == "Green" ]
then
echo "You have selected Green color"
if [ ${car} == "Audi" ]
then
echo " You have selected Green Audi"
fi
else
echo "Wrong Input"
fi