The Test in the UNIX Shell - The Case
(Page 4 of 4 )
The cases are an extension of the decision making. If we check if a statement is true or false, the case goes beyond this. A case assumes that there are multiple options available, and each one of them requires a different command line sequence. A case will have a single variable for which we will test what value it has from the list of the given values. The syntax of this is similar to that of the if.
case [the variable] in
[match_1]) ;;
[match_2]) ;;
...
[match_n]) ;;
esac
This type of case will only enter into the first valid match, unlike the one you may know from other programming languages. There is no need for break statements here. After entering into the first match, it will execute all of the command until the first double ; and continue afterward with the commands after the case. For example, initializing a variable according to the first argument of a script looks like this:
case "$1" in
0) alfa="zero";
echo Option zero is active;;
1) alfa="one";;
2) alfa="two";;
esac
The match can contain a meta-character also. We have three types of characters available for this. The * stands for anything. The ? stands for a single character that can be anything. For alternatives we may use the | character. The example below will describe this:
#!/bin/bash
#check if we have a single argument
if [ $# -ne 1 ]
then
echo Wrong Number of arguments
exit
fi
case $1 in
#exact match only
abc)
if rm abc 2>/dev/null
then
echo abc file deleted
fi;;
$this will match any string that contains 3 letter and the last two are bc
?bc) echo The first letter is wrong ;;
# a little alternation
a*c| ??)
echo The first argument is either containing two letters
echo Or it starts with a and finishes with c;;
esac
This will be all for today. Make sure you play around with these examples on your terminal and experiment with every one of the options. Learning all of the arguments is a damn hard task. Using them and playing with them repeatedly will make this titanic job child's play. After a time, it will jump right out at you when you need to use them, and then just go ahead and use them without needing to think too much about it. For this, all you need to do is to use them again and again.
Next week I will cover the loop structure inside UNIX shell programming. I will show you what kind of structures resolve this issue, what to use and how to use them so that you will end up with minimal code written and the task completed in your scripts. Please rate my article. Moreover, ask your questions in the blog, otherwise you may end up with dilemmas unanswered. Live with passion!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |