x
Loading
 Loading
Hello, Guest | Login | Register

The Case of the Matching Patterns

This month’s column continues our exploration of the bash shell’s scripting facilities by investigating the case statement. The case statement is particularly useful for handling the arguments of a script. It is both powerful and sophisticated, making it easy to express complex conditions that would tax your patience if you coded them using an if statement.

This month’s column continues our exploration of the bash shell’s scripting facilities by investigating the case statement. The case statement is particularly useful for handling the arguments of a script. It is both powerful and sophisticated, making it easy to express complex conditions that would tax your patience if you coded them using an if statement.

The case Statement

Both if and case are conditional statements; that is, you can use them to take alternative actions based on tests you specify. The power of the case statement lies in the kinds of tests it lets you specify. While the if statement relies on a true/false value, the case statement lets you specify a text string and a series of regular expressions, which it attempts to match against the text string. When a match is found, a series of commands is executed. Here’s the general form of the case statement:

case string in pattern) commands;; pattern) commands;; … esac 

You can see that the statement begins and ends with the keywords case and esac (the word case spelled backwards). Within the statement, string specifies the value to be matched, followed by one or more patterns to be matched against the string. An indefinite number of pattern lines may be included in a case statement. Note that each pattern line contains a series of commands. Each command within a series is separated from the next by a semi-colon (;), and the entire series…

Please log in to view this content.

Not Yet a Member?

Register with LinuxMagazine.com and get free access to the entire archive, including:

  • Hands-on Content
  • White Papers
  • Community Features
  • And more.
Already a Member?
Log in!
Username

Password

Remember me

Forgotten your password?
Forgotten your username?
Read More
  1. Helpful Tools for Software Developers
  2. The Github Hall of Fame
  3. Book'em, Github.
  4. This Week on Github: Stupid Ruby Tricks
  5. A Veritable Scatter Shot!
Follow Linux Magazine
Rackspace