Conditional Statements (Example)

 [prabhucloudxlab@cxl$n4 ~]$ cat>ifelse.sh

#Bash Script
#! /bin/bash
#-----------IF-ELSE-FI
#-----------Compare two numbers

echo "enter the a value:"
read a
echo "enter the b value:"
read b

if [$a==$b];
then
echo "--------------->>a value is equal to b value"
echo "                                  "
else
echo "--------------->>a value is NOT equal to b value"
echo "                                  "
fi


# TRUE && TRUE---------->> And condition
if [ 8 -gt 6 ] && [ 10 -eq 10 ];
then
echo "AND Conditions are true. --------------->>note:Both condtions should be true in AND condition.... 8>6 and 10=10"
echo "                            "
fi



# TRUE || FALSE---------->> OR condition
if [ 8 -gt 7 ] || [ 10 -eq 3 ];
then
echo " Condition is true.--------------->>Note: 8>7 but 10!=3, but one condition is true enough in OR condition"
echo "                                 "
fi


# TRUE && FALSE || FALSE || TRUE---------->> AND & OR conditions
if [[ 10 -eq 10 && 5 -gt 4 || 3 -eq 4 || 3 -lt 6 ]];
then
echo "Condition is true.--------------->>Note: 10 =10 && 5>4, AND condition true....if OR condition is False also no problem, if statement will execute successfully"
echo "                                  "
fi



#Nested if statement------------------>> Nested IF Statement
if [ $1 -gt 50 ]
then
                echo "Number is greater than 50."
                if (( $1 % 2 == 0 ))
                then
                echo "and it is an even number."
                fi
                if (( $1 % 2 == 1 ))
                then
                echo "and it is an odd number."
                fi
fi



Output:
[prabhucloudxlab@cxl$n4 ~]$ sh ifelse.sh 55
enter the a value:
50
enter the b value:
60
--------------->>a value is NOT equal to b value



Note:

AND Conditions are true. --------------->>
note: Both conditions should be true in AND condition.... 8>6 and 10=10


 OR condition--------------->>
 Condition is true.--------------->>Note: 8>7 but 10!=3, but one condition is true enough in OR condition

Condition is true.--------------->>Note: 10 =10 && 5>4, AND condition true....if OR condition is False also no problem, if statement will execute successfully

Number is greater than 50.
and it is an odd number.