View and vote on the article here: Shell Programming Series(XIV)
Shell Programming Series(XIV)| Category | | | Summary | <TT>case</TT> Statements
If you need to test the same variable for multiple conditions, there is a more efficient and cleaner way of doing it than with if statements. The case statement takes a variable as an argument and then uses st |
| | Body | Following program uses case statements to create a random quote generator.
1. #!/bin/sh
2. # Random quote generator.
3. quote_num=`jot ?r 1 1 5`
4. case ?$quote_num? in
5. 1) echo
6. echo ??Until he extends his circle of compassion to ?
7. echo ?include all living things, man will not himself find peace.??
8. echo ? ? Albert Schweitzer?
9. echo ;;
10. 2) echo
11. echo ??With regard to excellence, it is not enough to know, but?
12. echo ?we must try to have and use it.??
13. echo ? ? Aristotle?
14. echo ;;
15. 3) echo
16. echo ??Imagination is more important than knowledge. Knowledge?
17. echo ?is limited. Imagination encircles the whole world.??
18. echo ? ? Albert Einstein?
19. echo ;;
20. 4) echo
21. echo ??It is not the strongest of the species that survive, nor?
22. echo ?the most intelligent, but the one most responsive to change.??
23. echo ? ? Charles Darwin?
24. echo ;;
25. esac
26. exit 0
Once again, we use jot in line 3 to generate a random number between 1 and 4. The case block begins on line 4. The syntax is case variable in, where variable is the name of the variable that the tests should be performed on. Line 5 begins the first test. Everything to the left of the paranthesis indicates the condition the condition that case test for. Like the if statement, case stops at the first match it comes to, executes the statements that go with that match, and then jumps down to esac. The end of the statements that go along with each condition is marked with a double semicolon. The double semicolon can be on a line by itself, or it can be placed on the same line as the last statement, as is done in the previous example.
The case statement also accepts shell wildcards. For example:
case ?$myvar? in
1) #statements to do for a
;;
b)#statements to do for b
;;
*)#statements to do for anything else
;;
esac
The previous code will check to see if $myvar is equal to a or b, and perform those statements if it is. If it is not, the last test is a wildcard that will match anything. So, if $myvar is not equal to a or b, the last group of statements will be performed.
Other wildcards supported by case include the?, which works the same way it does in the shell, and the pipe character, which allows case to accept a range of options. For example, Y | y) will accept either Y or y as a match for a test. You can also enclose multiple characters in brackets to match a range of characters. For example, [Yy] | [Yy] [Ee] [Ss]) will accept either ?y? or ?yes? as a match in any combination of upper- or lowercase letters.
You've probably seen Web sites that have features such as random quotes that display on the page at each load or a random picture that changes each time the page is reloaded. The program presented previously that used case to display the quotes is one way to do this. This is called a CGI program, and it is a way for the Web server to run external programs and then send the output of those programs over the Internet to a browser.
|
|
This article was imported from zZine. (original author: ismail)
There are no replies to this post yet.
|