This article will describe how case command/statement works in Unix/Linux. We will see how to use case statements/command in Menu Based program. Let say we have a shell script AirthMatic_Operation.sh
$ cat AirthMatic_Operation.sh
#!/bin/sh
echo "Enter a number first Number:"
read V_First_Num;
echo "Enter a number second Number:"
read V_Second_Num;
while : #--Infinite Loop
do
echo "#############################################"
echo "Enter 1 For addition:"
echo "Enter 2 For subtraction:"
echo "Enter 3 For multiplication:"
echo "Enter 4 For division:"
echo "Enter 5 For exit:"
echo "#############################################"
echo "Enter your Choice:"
read V_Choice;
case ${V_Choice} in
1) echo `expr ${V_First_Num} + ${V_Second_Num}` ;;
2) echo `expr ${V_First_Num} - ${V_Second_Num}` ;;
3) echo `expr ${V_First_Num} \* ${V_Second_Num}` ;;
4) echo `expr ${V_First_Num} / ${V_Second_Num}` ;;
5) exit;;
*) echo "INVALID Option...!!" ;;
esac
done;
Lets execute the shell script AirthMatic_Operation.sh
$ sh AirthMatic_Operation.sh
Enter a number first Number:
4
Enter a number second Number:
2
#############################################
Enter 1 For addition:
Enter 2 For subtraction:
Enter 3 For multiplication:
Enter 4 For division:
Enter 5 For exit:
#############################################
Enter your Choice:
1
Addition is:6
#############################################
Enter 1 For addition:
Enter 2 For subtraction:
Enter 3 For multiplication:
Enter 4 For division:
Enter 5 For exit:
#############################################
Enter your Choice:
1. read command is used to read the data from standard input. V_First_Num and V_Second_Num will hold the first & second value passed to the terminal
2. We have used the infinite loop for continuous operation means we can perform all the operation without executing shell script again and again for every operation.
3. while with colon <while :> is used for infinite loop.
4. case command/statement is used for multiple operation, we can do the same using if-else block also but readability of code is more in case.
5. * is the keyword, which represent any input.
6. We must end the case statements with double semi colon(;;)
7. exit is the keyword which is used to terminate the program execution.
7. exit is the keyword which is used to terminate the program execution.
It is not like that you can use number as input in case statement, you can also use word or string in case statement. See below example for the same.
V_Choice="U"
case ${V_Choice} in
U) echo "You Have Selected Unix" ;;
L) echo "You Have Selected Linux" ;;
W) echo "You Have Selected Window" ;;
Z) exit;;
*) echo "INVALID Option...!!" ;;
esac
Output: You Have Selected Unix
If you want to case insensitive, then use syntax as below:
V_Choice="u"
case ${V_Choice} in
U|u) echo "You Have Selected Unix" ;;
L|l) echo "You Have Selected Linux" ;;
W|w) echo "You Have Selected Window" ;;
Z|z) exit;;
*) echo "INVALID Option...!!" ;;
esac
Output: You Have Selected Unix
V_Choice="Unix"
case ${V_Choice} in
Unix) echo "You Have Selected Unix" ;;
Linux) echo "You Have Selected Linux" ;;
Window) echo "You Have Selected Window" ;;
None) exit;;
*) echo "INVALID Option...!!" ;;
esac
Output: You Have Selected Unix
Conclusion: case command is very handy command where we have to implement menu based interfaces, it increase the code readability and understanding.
Keep Reading, Keep Learning, Keep Sharing...!!
No comments:
Post a Comment