| United States-English |
|
|
|
![]() |
Shells: User's Guide: HP 9000 Computers > Chapter 21 Basic Shell ProgrammingConditional Statements |
|
POSIX and Korn Shell provide constructs that allow a script to execute a designated set of command lines only if a special condition is met. These are called conditional statements. Discussed in this section are the following conditional statements: test, if, case, select, for, and while. This command evaluates expr; if expr evaluates true, test returns a zero exit status. If expr evaluates false, test returns a nonzero exit status. Syntax is: test expr or [ expr ] As shown, the test command can be replaced by appropriately spaced brackets ([ ]). An extensive list of exprs are covered in the HP-UX Reference on the test(1) manual page. Four exprs limited to the POSIX and Korn Shell are:
One expr unique to the POSIX Shell is:
The conditional command [[ test_expression ]] may also be used, where test_expression is a combination of the above conditional primitives combined with the and operator, &&, the or operator, ||, and the negation operator, !. The if statement allows you to execute one or several commands if a certain condition exists. The syntax is: if command-line if checks for command_line true. (true means command_line returns 0.) If true, conditional_cmd_line1 executes; if not, conditional_cmd_line2 executes. The following if statement checks whether x equals hello. If so, Welcome is printed; if not, Goodbye is printed.
In the following example, the files in the current directory are tested using brackets around the expression. Using -x tests for an executable file. If the test returns true, the executable file's name is printed.
The case statement allows you to easily check conditions and then process a command line if that condition evaluates to true. The syntax is: case string in The first line receives a string which is checked against each of the patterns to see if it matches. If the pattern matches, the command-list directly following is executed. For example:
The case statement first checks $i against each option for a match. If it matches -d or -r, the directory is removed (the | specifies logical OR). If it matches -o or -* (all others), an appropriate response is printed. If the string does not begin with -, no action is taken. This is a command unique to the POSIX and Korn Shell. It prints on the screen a set of words each preceded by a number. It then prints the PS3 prompt and reads into the REPLY variable the line typed by the user. If the contents of that line is the number of one of the listed words, the value of parameter is set to the corresponding word. (If the line begins with anything else, parameter is set to the null string.) Then, regardless of whether the user's input matches one of the words, the command_lines execute. (Within command_lines, conditionals can trap the nonmatches.) If the user presses Return but has input nothing, the command reprompts for input. The loop continues until it encounters a break. The syntax is: select parameter in words In the following example all the colors in words are printed out with a number in front. The default PS3 prompt, #?, is printed and the shell waits for a number. After Return is pressed, it echos that the number's corresponding color is an RGB color, and then prompts for the next entry. It continues prompting until Break is pressed, or it receives an interrupt. If the input is 4, or anything that is not set to a color, it returns is an RGB color without a preceding color name. If the user presses Return , but has input nothing, the command reprompts for input.
The for loop allows you to execute a command_line once for every new value assigned to a parameter in a specified list. Syntax is: for parameter [in list] In the following example, the first time through the loop, the for statement sets file to x and prints it out. The second time through the loop, y is printed out and the last time, z is printed out. When the list is completely finished, the loop is exited.
This loop continues executing command_line and processing through the list as long as the item in list continues to evaluate true. Once an item evaluates false, the loop is exited. The syntax is: while list
The loop in the following example initializes the variable x, and then increments and prints out the value until it equals 5 and you exit the loop.
This command exits loops created by the keywords for, while, until, or select. The syntax is: break [n] If n is specified, it breaks out of n nested loops. The following script checks the list of files x, y, z, none for executable files and prints the first executable file it encounters. If none is executable, file is left set to none but it is not printed.
This command skips any lines following it in a for, while, until, or select loop until the next iteration of the loop. Syntax is: continue [n] If n is specified, then resume execution starting at the nth enclosing loop. This next script checks for all executable files. If the file is executable the continue statement skips both following echo statements and starts another loop. If the file is not executable, the script prints that it is not executable. If the file is executable, nothing is printed.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||