Automating frequent tasks

Making choices and testing input

To handle the command line options to our script, lines 79 to 93 run getopts in a while loop. As long as getopts continues to return an option, the body of the loop is executed: when getopts can no longer detect any options, the while loop fails. shift is then used to discard the options.

Embedded in the loop to get options, we see another kind of statement: a case statement. Immediately after it, on lines 81 to 83, we see an if statement. These are both mechanisms for choosing between two or more options. if depends on the return value of a test condition; case operates by matching patterns.

When we need to repeat an operation a variable number of times, we must check after each repetition to determine whether it has produced the desired result. If not, we may need to repeat the task again: otherwise, we may want to do something else. The if statement allows us to choose between alternative courses of actions; the test or [ ... ] command allows us to check whether a condition holds true. (The case statement can be used as a generalized form of if statement, for choosing between many options. We will deal with it later.)

Next topic: Choosing one of two options: the if statement
Previous topic: Repeating commands one or more times: the until loop

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003