UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 44.5 Test String Values with Bourne Shell case Chapter 44
Shell Programming for the Uninitiated
Next: 44.7 Exit Status of UNIX Processes
 

44.6 Pattern Matching in case Statements

A case statement (44.5) is good at string pattern matching. Its "wildcard" pattern-matching metacharacters work like the filename wildcards (1.16) in the shell, with a few twists. Here are some examples:

?)

Matches a string with exactly one character like a, 3, !, and so on.

?*)

Matches a string with one or more characters (a non-empty string).

[yY]|[yY][eE][sS])

Matches y, Y or yes, YES, YeS, etc. The | means "or."

/*/*[0-9])

Matches a file pathname, like /xxx/yyy/somedir/file2, that starts with a slash, contains at least one more slash, and ends with a digit.

'What now?')

Matches the pattern What now?. The quotes (8.14) tell the shell to treat the string literally: not to break it at the space and not to treat the ? as a wildcard.

"$msgs")

Matches the contents of the msgs variable. The double quotes let the shell substitute the variable's value; the quotes also protext spaces and other special characters from the shell. For example, if msgs contains first next, then this would match the same string, first next.

- JP


Previous: 44.5 Test String Values with Bourne Shell case UNIX Power ToolsNext: 44.7 Exit Status of UNIX Processes
44.5 Test String Values with Bourne Shell case Book Index44.7 Exit Status of UNIX Processes

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System