In the case of the first 'if statement', if a condition goes false, then the second 'if condition' is checked.
if [ condition ];
then
<commands>
elif [ condition ];
then
<commands>
else
<commands>
fi
-----------------------------------------------------------------------------------------------------------------------------
Example 2
----------------------------------------------------------------------------------------------------------------------------
#! bin/bash
# if-else-if statement
count=10
if (( $count < 9 ))
then
echo "the condition is true:"
elif (( $count <= 9 ))
then
echo " the condition is true"
else
echo " the condition is false"
fi
[prabhucloudxlab@cxln4 ~]$ sh hello.sh
the condition is false
#! bin/bash
# if-else-if statement
count=10
if (( $count < 9 ))
then
echo "the 1st condition is false:"
elif (( $count > 9 ))
then
echo " the 2nd condition is true"
else
echo " the 2n condition is false"
fi
[prabhucloudxlab@cxln4 ~]$ sh hello.sh
the 2nd condition is true
-----------------------------------------------------------------------------------------------------------------------------
Example 1
Following example consists of two different scenarios wherein the
first else-if statement, the condition is true, and in the second else-if statement,
the condition is false.
-----------------------------------------------------------------------------------------------------------------------------
--Bash Script
[haritaraka12078@cxln4 ~]$ cat>elif1.sh
#!/bin/bash
read -p "Enter a number of quantity:" num
if [ $num -gt 100 ];
then
echo "Eligible for 10% discount"
elif [ $num -lt 100 ];
then
echo "Eligible for 5% discount"
else
echo "Lucky Draw Winner"
echo "Eligible to get the item for free"
fi
[haritaraka12078@cxln4 ~]$ sh elif1.sh
Enter a number of quantity:120
Eligible for 10% discount
then
<commands>
elif [ condition ];
then
<commands>
else
<commands>
fi
-----------------------------------------------------------------------------------------------------------------------------
Example 2
----------------------------------------------------------------------------------------------------------------------------
#! bin/bash
# if-else-if statement
count=10
if (( $count < 9 ))
then
echo "the condition is true:"
elif (( $count <= 9 ))
then
echo " the condition is true"
else
echo " the condition is false"
fi
[prabhucloudxlab@cxln4 ~]$ sh hello.sh
the condition is false
-----------------------------------------------------------------------------------------------------------------------------
Example 2
----------------------------------------------------------------------------------------------------------------------------
Example 2
----------------------------------------------------------------------------------------------------------------------------
#! bin/bash
# if-else-if statement
count=10
if (( $count < 9 ))
then
echo "the 1st condition is false:"
elif (( $count > 9 ))
then
echo " the 2nd condition is true"
else
echo " the 2n condition is false"
fi
[prabhucloudxlab@cxln4 ~]$ sh hello.sh
the 2nd condition is true
-----------------------------------------------------------------------------------------------------------------------------
Example 1
Following example consists of two different scenarios wherein the
first else-if statement, the condition is true, and in the second else-if statement,
the condition is false.
-----------------------------------------------------------------------------------------------------------------------------
--Bash Script
[haritaraka12078@cxln4 ~]$ cat>elif1.sh
#!/bin/bash
read -p "Enter a number of quantity:" num
if [ $num -gt 100 ];
then
echo "Eligible for 10% discount"
elif [ $num -lt 100 ];
then
echo "Eligible for 5% discount"
else
echo "Lucky Draw Winner"
echo "Eligible to get the item for free"
fi
[haritaraka12078@cxln4 ~]$ sh elif1.sh
Enter a number of quantity:120
Eligible for 10% discount
[haritaraka12078@cxln4 ~]$ sh elif1.sh
Enter a number of quantity:80
Eligible for 5% discount
[haritaraka12078@cxln4 ~]$ sh elif1.sh
Enter a number of quantity:1000
Eligible for 10% discount
-----------------------------------------------------------------------------------------------------------------------------
Example 2
-----------------------------------------------------------------------------------------------------------------------------
-- Bash Script
[haritaraka12078@cxln4 ~]$ cat>elif2.sh
#!/bin/bash
read -p "Enter a number of quantity:" num
if [ $num -gt 200 ];
then
echo "Eligible for 20% discount"
elif [[ $num == 200 || $num == 100 ]];
then
echo "Lucky Draw Winner"
echo "Eligible to get the item for free"
elif [[ $num -gt 100 && $num -lt 200 ]];
then
echo "Eligible for 10% discount"
elif [ $num -lt 100 ];
then
echo "No discount"
fi
[haritaraka12078@cxln4 ~]$ sh elif2.sh
Enter a number of quantity:100
Lucky Draw Winner
Eligible to get the item for free
[haritaraka12078@cxln4 ~]$ sh elif2.sh
Enter a number of quantity:200
Lucky Draw Winner
Eligible to get the item for free
[haritaraka12078@cxln4 ~]$ sh elif2.sh
Enter a number of quantity:250
Eligible for 20% discount
[haritaraka12078@cxln4 ~]$ sh elif2.sh
Enter a number of quantity:80
No discount