case: case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac
Selectively execute COMMANDS based upon WORD matching PATTERN. The
'|' is used to separate multiple patterns.
(ok my pun was as poor a pun as that help is help)i don't use case much, i don't know why, it's just never "appealed" to me
"case" is kind of like a "for" and an "if" combined
what they are saying above (in a much convoluted way) is that the below script:
#!/bin/bash
echo -e "using letters type a number between zero and ten\n"
read -e
case "$REPLY" in
"zero" ) echo -e "you typed $REPLY which in number is 0\n" ;;
"one" ) echo -e "you typed $REPLY which in number is 1\n" ;;
"two" ) echo -e "you typed $REPLY which in number is 2\n" ;;
"three" ) echo -e "you typed $REPLY which in number is 3\n" ;;
"four" ) echo -e "you typed $REPLY which in number is 4\n" ;;
"five" ) echo -e "you typed $REPLY which in number is 5\n" ;;
"six" ) echo -e "you typed $REPLY which in number is 6\n" ;;
"seven" ) echo -e "you typed $REPLY which in number is 7\n" ;;
"eight" ) echo -e "you typed $REPLY which in number is 8\n" ;;
"nine" ) echo -e "you typed $REPLY which in number is 9\n"
echo -e "$REPLY*$REPLY=$[9*9]\n" ;;
"ten" ) echo -e "you typed $REPLY which in number is 10\n" ;;
* ) echo -e "something else was typed, you typed: $REPLY\n" ;;
esac
exit 0
yeah i am, read puts it's "input" into the variable $REPLY if it's not given a variable, personally i always write a variable there, but in this case i'm showing you a way to save some typing
i quote $REPLY because the rule of thumb i use is, if you compare to quoted, use quoted
ie, echo -e "you typed $REPLY which in number is 0\n"
which becomes "you typed zero which in number is 0" followed by a newline
no..i'm not, i just wanted to show you that the part between ) and ;; can be more than one line and more than one command
basically: "in case REPLY variable contains "nine", echo (just like zero through eight) but also...
echo -e "$REPLY*$REPLY=$[9*9]\n"
in other words "nine*nine=$[9*9] newline"
the $[9*9] part multiplies 9 with 9 and becomes the result, could i have put 81 there? ..sure i could..but then you wouldn't know that you could do maths on the commandline or in a script, would ya?
unfortunately not much practical use exists for the exact above script (i'm sorry i tend to try to make "useful" learner scripts, in this case i couldn't really figure one out).
with a little thought though, the possibilities are large. In fact, I've noticed that since/during writing this 101-chapter 7 i've started to use case more and more
umm okay but see i don't trust my users, even if i specify use only small letters or CAPS...
ahh well the problem above is one of case sensitivity (always when we use typed input of keywords we need to either check for the different versions). IE at least "ONE", "One", "one"
whilst that could be done: (loosely written..ya'll know the syntax by now)
if test "$REPLY"="ONE"
then
commands here (echo you entered "one")
elif ( "One")
then
commandblock
elif ( "one" )
commandblock
elif (now you do the 3 iterations for two TWO Two)....and then three and then *JEESSEEEEE*
fi
or you could even do a:
REPLY_FIX=`echo -e $REPLY | sed y/ABCDEFGHIJKLMNOPQRSTUVWXYX/abcdefghijklmnopqrstuvwxyz/`
and then search for small (non capital) "one" "two" and so on
they are both kind of clumsly when compared to something along the lines of:(ok.. so the sed solution has some style points for longer comparison lists)
case "$REPLY" in "ZERO" | "Zero" | "zero" ) echo -e "you typed $REPLY which in number is 0\n" ;; "ONE" | "One" | "one" ) echo -e "you typed $REPLY which in number is 1\n" ;;
i'm leaving this chapter "open" (being written) for a while, in "case" i find more examples
| Bash scripting 101: Chapter 6: can you html (putting some things together) | Bash Scripting101: Chapter 8: dare you risk some misc? |