IF ELSE
Sometimes, we want to process a specific set of statements if a condition is true, and another set of statements if it is false.
Sometimes, we want to process a specific set of statements if a condition is true, and another set of statements if it is false.
To perform such type of actions, we can apply the if-else mechanism.
if [ condition ];
then
<if block commands>
else
<else block commands>
fi
=================================================================
Example
=================================================================
# if-else statement
count=10
if [ $count -eq 9 ] ##Syntax 2: We can give [ $count -gt 9 ] or (( $count > 9 ))
then
echo "the condition is true:"
else
echo " the condition is false"
fi
the condition is false
[prabhucloudxlab@cxln4 ~]$ cat>hello.sh
#! bin/bash
[prabhucloudxlab@cxln4 ~]$ sh hello.sh
: '
NOTE:
: '
NOTE:
-ne ->> Not equal
-gt ->> grether than
-ge ->> grether than are equal to
-lt ->> less than
-le ->> less than are equal to
=================================================================
Example
=================================================================
[haritaraka12078@cxln4 ~]$ cat>ifelse1.sh
#!/bin/bash
#when the condition is true
if [ 10 -gt 3 ];
then
echo "10 is greater than 3."
else
echo "10 is not greater than 3."
fi
#when the condition is false
if [ 3 -gt 10 ];
then
echo "3 is greater than 10."
else
echo "3 is not greater than 10."
fi
[haritaraka12078@cxln4 ~]$ cat>ifelse1.sh
#!/bin/bash
#when the condition is true
if [ 10 -gt 3 ];
then
echo "10 is greater than 3."
else
echo "10 is not greater than 3."
fi
#when the condition is false
if [ 3 -gt 10 ];
then
echo "3 is greater than 10."
else
echo "3 is not greater than 10."
fi
[haritaraka12078@cxln4 ~]$ chmod 777 ifelse1.sh
[haritaraka12078@cxln4 ~]$ sh ifelse1.sh
10 is greater than 3.
3 is not greater than 10.
=================================================================
Example
=================================================================
[haritaraka12078@cxln4 ~]$ cat>ifelse2.sh
#!/bin/bash
# When condition is true
# TRUE && FALSE || FALSE || TRUE
if [[ 10 -gt 9 && 10 == 9 || 2 -lt 1 || 25 -gt 20 ]];
then
echo "Given condition is true."
else
echo "Given condition is false."
fi
# When condition is false
#TRUE && FALSE || FALSE || TRUE
if [[ 10 -gt 9 && 10 == 8 || 3 -gt 4 || 8 -gt 8 ]];
then
echo "Given condition is true."
else
echo "Given condition is not true."
fi
[haritaraka12078@cxln4 ~]$ chmod 777 ifelse2.sh
[haritaraka12078@cxln4 ~]$ sh ifelse2.sh
Given condition is true.
Given condition is not true.
=================================================================
Example
=================================================================
[haritaraka12078@cxln4 ~]$ cat>ifelse3.sh
#!/bin/bash
read -p "Enter a value:" value
if [ $value -gt 9 ];
then
echo "The value you typed is greater than 9."
else
echo "The value you typed is not greate"
fi
[haritaraka12078@cxln4 ~]$ sh ifelse3.sh
Enter a value:25
The value you typed is greater than 9.
[haritaraka12078@cxln4 ~]$ sh ifelse3.sh
Enter a value:8
The value you typed is not greate
=================================================================
Example
=================================================================
[haritaraka12078@cxln4 ~]$ cat>ifelse4.sh
#!/bin/bash
read -p "Enter a value:" value
if [ $value -gt 9 ];
then
if [ $value -lt 11 ];
then
echo "$value>9, $value<11"
else
echo "The value you typed is greater than 9."
fi
else
echo "The value you typed is not greater than 9."
fi
[haritaraka12078@cxln4 ~]$ chmod 777 ifelse4.sh
[haritaraka12078@cxln4 ~]$ sh ifelse4.sh
Enter a value:9
The value you typed is not greater than 9.
[haritaraka12078@cxln4 ~]$ sh ifelse4.sh
Enter a value:8
The value you typed is not greater than 9.
[haritaraka12078@cxln4 ~]$ sh ifelse4.sh
Enter a value:25
The value you typed is greater than 9.
'
https://www.javatpoint.com/bash-if-else-statement
#=======================================================