A basic if statement commands that if a particular condition is true, then only execute a given set of actions
if [ expression ];
then
statements
fi
The statement between then and fi (If backwards) will be executed only if the expression (between the square brackets) is true.
---------------------------------------------------------------------------------------------------------------------------
Example:
---------------------------------------------------------------------------------------------------------------------------
#! bin/bash
# if - statement
count=10
if [ $count -eq 10 ] #space should be give both sides, other wise it will give read error
then
echo "the condition is true:"
fi
[prabhucloudxlab@cxln4 ~]$ sh hello.sh
the condition is true:
---------------------------------------------------------------------------------------------------------------------------
Example:
---------------------------------------------------------------------------------------------------------------------------
[prabhucloudxlab@cxln4 ~]$ cat>hello.sh#! bin/bash
count=10
if [ $count -eq 9 ] #space should be give both sides, other wise it will give read error
then
echo "the condition is true:"
fi
[prabhucloudxlab@cxln4 ~]$ sh hello.sh
Note: Condition is false. but It will not give any output, because we don't have else condition.
---------------------------------------------------------------------------------------------------------------------------
Example
---------------------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ cat>if.sh
#!/bin/bash
read -p " Enter number : " number
if [ $number -gt 125 ]
then
echo "Value is greater than 125"
fi
[haritaraka12078@cxln4 ~]$ sh if.sh
Enter number : 150
Value is greater than 125
---------------------------------------------------------------------------------------------------------------------------
Example
---------------------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ cat>if2.sh
#!/bin/bash
# if condition is true
if [ "myfile" == "myfile" ];
then
echo "true condition"
fi
# if condition is false
if [ "myfile" == "yourfile" ];
then
echo "false condition"
fi
[haritaraka12078@cxln4 ~]$ sh if2.sh
true condition
---------------------------------------------------------------------------------------------------------------------------
Example
---------------------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ cat>if3.sh
#!/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
[haritaraka12078@cxln4 ~]$ chmod 777 if3.sh
[haritaraka12078@cxln4 ~]$ sh if3.sh
10 is greater than 3.
3 is less than 10.
10 is equal to 10.
---------------------------------------------------------------------------------------------------------------------------
Example : AND &&
---------------------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ cat>ifand.sh
#!/bin/bash
# TRUE && TRUE
if [ 8 -gt 6 ] && [ 10 -eq 10 ];
then
echo "Conditions are true"
fi
# TRUE && FALSE
if [ "mylife" == "mylife" ] && [ 3 -gt 10 ];
then
echo "Conditions are false"
fi
[haritaraka12078@cxln4 ~]$ chmod 777 ifand.sh
[haritaraka12078@cxln4 ~]$ sh ifand.sh
Conditions are true
---------------------------------------------------------------------------------------------------------------------------
Example: OR ||
---------------------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ cat ifor.sh
#!/bin/bash
# TRUE || FALSE
if [ 8 -gt 7 ] || [ 10 -eq 3 ];
then
echo " Condition is true. "
fi
# FALSE || FALSE
if [ "mylife" == "yourlife" ] || [ 3 -gt 10 ];
then
echo " Condition is false. "
fi
[haritaraka12078@cxln4 ~]$ sh ifor.sh
Condition is true.
---------------------------------------------------------------------------------------------------------------------------
Example: AND OR
---------------------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ cat ifandor.sh
#!/bin/bash
# TRUE && FALSE || FALSE || TRUE
if [[ 10 -eq 10 && 5 -gt 4 || 3 -eq 4 || 3 -lt 6 ]];
then
echo "Condition is true."
fi
# TRUE && FALSE || FALSE
if [[ 8 -eq 8 && 8 -gt 10 || 9 -lt 5 ]];
then
echo "Condition is false"
fi
[haritaraka12078@cxln4 ~]$ chmod 777 ifandor.sh
[haritaraka12078@cxln4 ~]$ sh ifandor.sh
Condition is true.
---------------------------------------------------------------------------------------------------------------------------
Example: NESTED IF
---------------------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ cat>ifnest.sh
#!/bin/bash
#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
[haritaraka12078@cxln4 ~]$ sh ifnest.sh 55
Number is greater than 50.
and it is an odd number.
[haritaraka12078@cxln4 ~]$ sh ifnest.sh 100
Number is greater than 50.
and it is an even number.
For More details please check in below link
https://www.javatpoint.com/bash-if-statement
=====================================================================