bash if bash elif bash case statement

Conditionals statement in bash or shell script

Conditionals statement in bash or shell script

conditional statements is used for making correct decisions and perform the right actions on it.

Unix Shell bash if bash elif condition is follow

if…fi statement
if…else…fi statement
if…elif…else…fi statement

case…esac statement

if [conditional expression]; then
Statement or command if ‘conditional expression’ is true.
fi

#!/bin/bash
echo "Enter numeric value: "
read value

if [ $value -gt 10 ]; then
echo "Value is greater than 10"
fi

if [conditional expression]; then
Statement or command if ‘conditional expression’ is true.
else
Statement or command if ‘conditional expression’ is false.
fi

#!/bin/bash
echo "Enter numeric value: "
read value

if [ $value -gt 10 ]; then
echo "Value is greater than 10"
else
echo "Value is less than 10"
fi

bash if bash elif

If [ conditional expression1 ] then
statement1
statement2
.
elif [ conditional expression2 ] then
statement3
statement4
.
.
.
else
statement5
fi

#!/bin/bash
Num=55
if [ $Num-eq 60 ]
then
echo "Number is 60"
elif [ $Num-gt 60 ]
then
echo "Number is greater than 60"
else
echo "Number is less than 60"
fi

Syntax of bash case statement.

case $variable in
pattern-1)
commands
;;
pattern-2)
commands
;;
pattern-3|pattern-4|pattern-5)
commands
;;
pattern-N)
commands
;;
*)
commands
;;
esac

Example

 

#!/bin/bash
echo "Please Enter Number between 1 to 3 "
read Num

case $Nmin
1)
echo "You Have enter One"
;;
2)
echo "You Have enter Two"
;;
3)
echo "You Have enter Three"
;; 
*)
echo "Sorry you have enter wrong nmber"
;;
esac

You May Also Enjoy Reading This …

Leave a Reply

Your email address will not be published. Required fields are marked *