Command Syntax

   

Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents
Appendix H.  Pdksh Man Page


The shell begins parsing its input by breaking it into words. Words, which are sequences of characters, are delimited by unquoted white-space characters (space, tab and newline) or meta-characters (<, >, |, ;, &, (and)). Aside from delimiting words, spaces and tabs are ignored, while newlines usually delimit commands. The metacharacters are used in building the following tokens: <, <&, <<, >, >&, >>, etc. are used to specify redirections (see Input/Output Redirection below); | is used to create pipelines; |& is used to create co-processes (see Co-Processes later in this appendix); ; is used to separate commands; & is used to create asynchronous pipelines; && and || are used to specify conditional execution; ;; is used in case statements; ((..)) are used in arithmetic expressions; and lastly, (..) are used to create subshells.

Whitespace and metacharacters can be quoted individually using backslash (\), or in groups using double (") or single (') quotes. Note that the following characters are also treated specially by the shell and must be quoted if they are to represent themselves: \, ", ', #, $, `, ~, {,}, *, ? and [. The first three of these are the above mentioned quoting characters (see Quoting later in this appendix); #, if used at the beginning of a word, introduces a comment - everything after the # up to the nearest newline is ignored; $ is used to introduce parameter, command and arithmetic substitutions (see Substitution later in this appendix); ' introduces an old-style command substitution (see Substitution below); ~ begins a directory expansion (see Tilde Expansion later in this appendix); {and} delimit csh(1) style alternations (see Brace Expansion later in this appendix); and, finally, *, ? and [ are used in file name generation (see File Name Patterns later in this appendix).

As words and tokens are parsed, the shell builds commands, of which there are two basic types: simple-commands, typically programs that are executed, and compound-commands, such as for and if statements, grouping constructs and function definitions.

A simple-command consists of some combination of parameter assignments (see Parameters later in this appendix), input/output redirections (see Input/Output Redirections later in this appendix), and command words; the only restriction is that parameter assignments come before any command words. The command words, if any, define the command that is to be executed and its arguments. The command may be a shell builtin command, a function or an external command, i.e., a separate executable file that is located using the PATH parameter (see Command Execution later in this appendix). Note that all command constructs have an exit status: for external commands, this is related to the status returned by wait(2) (if the command could not be found, the exit status is 127, if it could not be executed, the exit status is 126); the exit status of other command constructs (built-in commands, functions, compound-commands, pipelines, lists, etc.) are all well defined and are described where the construct is described. The exit status of a command consisting only of parameter assignments is that of the last command substitution performed during the parameter assignment or zero if there were no command substitutions.

Commands can be chained together using the | token to form pipelines, in which the standard output of each command but the last is piped to the standard input of the following command. The exit status of a pipeline is that of its last command. A pipeline may be prefixed by the ! reserved word which causes the exit status of the pipeline to be logically complemented: if the original status was 0 the complemented status will be 1, and if the original status was not 0, then the complemented status will be 0.

Lists of commands can be created by separating pipelines by any of the following tokens: &&, ||, &, |& and ;. The first two are for conditional execution: cmd1 && cmd2 executes cmd2 only if the exit status of cmd1 is zero; || is the opposite - cmd2 is executed only if the exit status of cmd1 is non-zero. && and || have equal precedence which is higher than that of &, |& and ;, which also have equal precedence. The & token causes the preceding command to be executed asynchronously, that is, the shell starts the command, but does not wait for it to complete (the shell does keep track of the status of asynchronous commands - see Job Control later in this appendix). When an asynchronous command is started when job control is disabled (i.e., in most scripts), the command is started with signals INT and QUIT ignored and with input redirected from /dev/null (however, redirections specified in the asynchronous command have precedence). The |& operator starts a co-process which is special kind of asynchronous process (see Co-Processes later in this appendix). Note that a command must follow the && and || operators, while a command need not follow &, |& and ;. The exit status of a list is that of the last command executed, with the exception of asynchronous lists, for which the exit status is 0.

Compound commands are created using the following reserved words - these words are only recognized if they are unquoted and if they are used as the first word of a command (i.e., they can't be preceded by parameter assignments or redirections):


case else function then ! do esac if time [[ done fi in until 
{elif for select while} 

Note: Some shells (but not this one) execute control structure commands in a subshell when one or more of their file descriptors are redirected, so any environment changes inside them may fail. To be portable, the exec statement should be used instead to redirect file descriptors before the control structure.

In the following compound command descriptions, command lists (denoted as list) that are followed by reserved words must end with a semi-colon, a newline or a (syntactically correct) reserved word. For example,


{echo foo; echo bar;} 
{echo foo; echo bar} 
{{echo foo; echo bar;}} 

are all valid, but


{echo foo; echo bar} 

is not.

(list)

Execute list in a subshell. There is no implicit way to pass environment changes from a subshell back to its parent.

{list}

Compound construct; list is executed, but not in a subshell. Note that { and } are reserved words, not meta-characters.

case word in [ [(] pattern [| pattern] ...) list;; ] ... esac

The case statement attempts to match word against the specified patterns; the list associated with the first successfully matched pattern is executed. Patterns used in case statements are the same as those used for file name patterns except that the restrictions regarding . and / are dropped. Note that any unquoted space before and after a pattern is stripped; any space with a pattern must be quoted. Both the word and the patterns are subject to parameter, command, and arithmetic substitution as well as tilde substitution. For historical reasons, open and close braces may be used instead of in and esac (e.g., case $foo {*) echo bar; }). The exit status of a case statement is that of the executed list; if no list is executed, the exit status is zero.

for name [ in word... term] do list done

where term is either a newline or a ;. For each word in the specified word list, the parameter name is set to the word and list is executed. If in is not used to specify a word list, the positional parameters ("$1", "$2", etc.) are used instead. For historical reasons, open and close braces may be used instead of do and done (e.g., for i; {echo $i;}). The exit status of a for statement is the last exit status of list; if list is never executed, the exit status is zero.

if list then list [elif list then list] ... [else list] fi

If the exit status of the first list is zero, the second list is executed; otherwise the list following the elif, if any, is executed with similar consequences. If all the lists following the if and elifs fail (i.e., exit with non-zero status), the list following the else is executed. The exit status of an if statement is that of non-conditional list that is executed; if no non-conditional list is executed, the exit status is zero.

select name [ in word... term] do list done

where term is either a newline or a ;. The select statement provides an automatic method of presenting the user with a menu and selecting from it. An enumerated list of the specified words is printed on standard error, followed by a prompt (PS3, normally '#? '). A number corresponding to one of the enumerated words is then read from standard input, name is set to the selected word (or is unset if the selection is not valid), REPLY is set to what was read (leading/trailing space is stripped), and list is executed. If a blank line (i.e., zero or more IFS characters) is entered, the menu is re-printed without executing list. When list completes, the enumerated list is printed if REPLY is null, the prompt is printed and so on. This process is continues until an end-of-file is read, an interrupt is received or a break statement is executed inside the loop. If in word... is omitted, the positional parameters are used (i.e., "$1", "$2", etc.). For historical reasons, open and close braces may be used instead of do and done (e.g., select i; {echo $i; }). The exit status of a select statement is zero if a break statement is used to exit the loop, non-zero otherwise.

until list do list done

This works like while, except that the body is executed only while the exit status of the first list is non-zero.

while list do list done

A while is a prechecked loop. Its body is executed as often as the exit status of the first list is zero. The exit status of a while statement is the last exit status of the list in the body of the loop; if the body is not executed, the exit status is zero.

function name {list}

Defines the function name. See Functions later in this appendix. Note that redirections specified after a function definition are performed whenever the function is executed, not when the function definition is executed.

name () command

Mostly the same as function. See Functions later in this appendix.

time [ -p ] [pipeline]

The time reserved word is described in the Command Execution section.

((expression))

The arithmetic expression expression is evaluated; equivalent to let "expression". See Arithmetic Expressions and the let command later in this appendix.

[[expression]]

Similar to the test and [ ... ] commands (described later), with the following exceptions: Field splitting and file name generation are not performed on arguments. The -a (and) and -o (or) operators are replaced with && and ||, respectively. Operators (e.g., -f, =, !, etc.) must be unquoted. The second operand of != and = expressions are patterns (e.g., the comparison in [[ foobar = f*r ]] succeeds). There are two additional binary operators: < and > which return true if their first string operand is less than, or greater than, their second string operand, respectively. The single argument form of test, which tests if the argument has non-zero length, is not valid - explicit operators must be always be used, e.g., instead of [str] use [[ -n str]] Parameter, command and arithmetic substitutions are performed as expressions are evaluated and lazy expression evaluation is used for the && and || operators. This means that in the statement [[ -r foo && $(< foo) = b*r ]] the $(< foo) is evaluated if and only if the file foo exists and is readable.


       
    Top
     



    Korn Shell. Unix and Linux Programming Manual, Third Edition
    Korn Shell. Unix and Linux Programming Manual, Third Edition
    ISBN: N/A
    EAN: N/A
    Year: 2000
    Pages: 177

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net