44.5 Test String Values with Bourne Shell caseEach time you type a command line at a shell prompt, you can see what happens and decide what command to run next. But a shell script needs to make decisions like that itself. A case statement helps the script make decisions. A case statement compares a string (usually taken from a shell or environment variable ( 6.8 , 6.1 ) ) to one or more patterns. The patterns can be simple strings (words, digits, etc.) or they can be case wildcard expressions ( 44.6 ) . When the case finds a pattern that matches the string, it executes one or more commands. Here's an example. It tests your TERM ( 5.10 ) environment variable. If you're using a vt100 or tk4023 terminal, it runs a command to send some characters to your terminal. If you aren't on either of those, it prints an error and quits:
Here are more details about how this works.
The statement compares the string between the words
If the first pattern doesn't match, the shell tries the next
case - here,
tk4023
.
As above, a match runs the command and jumps to the
esac
.
No match?
The next pattern is the wildcard You can use as many patterns as you want to. The first one that matches is used. It's okay if none of them match. The style doesn't matter much. Pick one that's readable and be consistent. - |
|