Korn shell compound commands
A compound command can be a list of simple commands or a pipeline, or it can begin with a reserved word. Most of the time, you will use compound commands such as if, while, and for when you are writing shell scripts.
Command syntax | Description |
---|---|
for Identifier [in Word ...] ;do List ;done | Each time a for command is executed, the Identifier parameter is set to the next word taken from the in Word ... list. If the in Word ... command is omitted, then the for command executes the do List command once for each positional parameter that is set. Execution ends when there are no more words in the list. |
select Identifier [in Word ...] ;do List ;done | A select command prints
on standard error (file descriptor 2) the set of words specified, each preceded
by a number. If the in Word ... command is omitted,
then the positional parameters are used instead. The PS3 prompt is
printed and a line is read from the standard input. If this line consists
of the number of one of the listed words, then the value of the Identifier parameter
is set to the word corresponding to this number. If the line read from standard input is empty, the selection list is printed again. Otherwise, the value of the Identifier parameter is set to null. The contents of the line read from standard input is saved in the REPLY parameter. The List parameter is executed for each selection until a break or an end-of-file character is encountered. |
case Word in [[ ( ] Pattern [ | Pattern] ... ) List ;;] ... esac | A case command executes the List parameter associated with the first Pattern parameter that matches the Word parameter. The form of the patterns is the same as that used for file name substitution. |
if List ;then List [elif List ;then List] ... [;else List] ;fi | The List parameter specifies
a list of commands to be run. The shell executes the if List command
first. If a zero exit status is returned, it executes the then List command.
Otherwise, the commands specified by the List parameter
following the elif command are executed. If the value returned by the last command in the elif List command is zero, the then List command is executed. If the value returned by the last command in the then List command is zero, the else List command is executed. If no commands specified by the List parameters are executed for the else or then command, the if command returns a zero exit status. |
while List ;do List ;done until List ;do List ;done | The List parameter specifies a list of commands to be run. The while command repeatedly executes the commands specified by the List parameter. If the exit status of the last command in the while List command is zero, the do List command is executed. If the exit status of the last command in the while List command is not zero, the loop terminates. If no commands in the do List command are executed, then the while command returns a zero exit status. The until command might be used in place of the while command to negate the loop termination test. |
( List) | The List parameter specifies
a list of commands to run. The shell executes the List parameter
in a separate environment. Note: If two adjacent open parentheses are needed
for nesting, you must insert a space between them to differentiate between
the command and arithmetic evaluation.
|
{ List;} | The List parameter specifies
a list of commands to run. The List parameter is simply
executed. Note: Unlike the metacharacters
( ) , {
} denote reserved words (used for special purposes, not as user-declared
identifiers). To be recognized, these reserved words must appear at the beginning
of a line or after a semicolon (; ). |
[[Expression]] | Evaluates the Expression parameter. If the expression is true, then the command returns a zero exit status. |
function Identifier { List ;} or function Identifier () {List ;} | Defines a function that is referred to by
the Identifier parameter. The body of the function is the
specified list of commands enclosed by { } . The (
) consists of two operators, so mixing blank characters with the identifier, ( and ) is permitted, but is not necessary. |
time Pipeline | Executes the Pipeline parameter. The elapsed time, user time, and system time are printed to standard error. |