• Keine Ergebnisse gefunden

Controlling the Flow or Control

Im Dokument The Chapter (Seite 21-24)

The shell provides several commands that implement a variety of tontrol structures useful in controlling the flow of control in shell procedures. Before describing these structures, a few terms need to be defined.

A ,imple commcuulis any single irreducible command specified by the name of an executable file. 1/0 redirection arguments can appear in a simple command line and are passed to the shell, tI ot to the command.

A command is a simple command or any of the shell control comm&nds described below. A pipelin.e is a. sequence of one or more commands sepa.rated by vertical bars (

I).

In a pipeline, the standard output of each command but the last is connected (by a pipe) to the standard input of the next command.

Each command in a pipeline is run separately; the shell waits for the last command to finish. The exit status or a pipeline is nonzero if the exit status of either the first or last process in the pipeline is nonzero.

A C omman.tlli8t is a sequence of one or more pipelines separated by a semicolon (;), an ampersand (&), an "and-ir' symbol (&&), or an "or-ir'

(II )

symbol, and optionally terminated by a semicolon or an ampersand. A semicolon causes sequential execution or the previous pipeline. This means that the shell waits for the pipeline to finish berore reading the next pipeline. On the other hand, the ampersand (&) causes asynchronous background execution of the preceding pipeline. Thus, both sequential and background execution are allowed. A background pipeline continues execution until it terminates voluntarily, or until its processes are kill-ed.

Other uses of the ampersand include off-line printing, background compilation, and generation of jobs to be sent to other computers. For example, iryou type

nohup cc prog.c&

you may continue working while the C compiler runs in the background. A command line ending with an ampersand is immune to interrupts or quits that you might generate by typing INTERRUPT or QUIT. It is also immune to logouts with CNTRL-D. However, CNTRL-D wiU abort the command if you are operating over a dial-up line. In this case, it is wise to make the command immune to hang-ups (Le., logouts) as well. The nohup command is used for this purpose. In the above example without nohup, if you logout from a dial-up line while cc is still executing, cc will be killed and your output will disappear.

The ampersand operator should be used with restraint, especially on heavily·

loaded systems. Other users will not consider you a good citizen if you start up a large number of background processes without a compelling reason for doing so.

7-1g

The and-if and or-if (&& and

II)

operators cause conditional execution of pipelines. Both of these are of equal precedence when evaluating command lines (but both are lower than the ampersand

(&)

and the vertical bar (I )). In the command line

cmdl

II

cmd2

the first command, tmdl, is executed and its exit status examined. Only it tmdl rails (i.e., has a nonzero exit status) is cmdtexecuted. Thus, this is a more terse notation for:

it cmdl

test $1 ! -0 then

cmd2 fi

The and-if operator (&&) operator yields a complementary test. For example, in the following command line

cmdl && cmd2

the second command is executed only it the first ,veeeed, (and has a zero exit status). In the sequence below, each command is' executed in order until one fails:

cmdl && cmd2 && cmd3 && ... && cmdn

A simple command in a pipeline may be replaced by a command list enclosed in either parentheses or braces. The output of all the commands so enclosed is combined into one stream that becomes the input to the next command in the pipeline. The rollowing line formats and prints two separate documents:

{ nroft' -mm text1; nroft' -mm text2j } Ilpr

Note that a space is needed arter the left brace and that a semicolon should appear.berore the right brace.

7.0.1 Using the it Statement

The shell provides structured conditional capability with the it command. The simplest it command has the rollowing form:

it command-lilt then command-li,t fl

The command list rollowing the itisexecuted and itthe last commandin the list has a zero exit status, then the command list that follows then is executed. The

7-20

word fl indicates the end or the ir command.

To cause an alternative set or commands to be executed when there is a nonzero exit status, an else clause can be given with the following structure:

it comma"tl-lilt then t omma"tl-li.t else t omma"tl-lilt fl

Multiple tests can be achieved in an ir command by using the elir clause, although the case statement (See Section 1.G.2) is better for large numbers of tests. For example:

The above example is executed as follows: if the value of the first positional parameter is a filename (-f), then print that file; if not, then check to see if it is the name ofa directory (-d). Irso, change to that directory (cd) and print all the files there (pr.). Otherwise, echo the error message.

The ir command may be nested (but be sure to end each one with a

6).

The new lines in the above examples of ir may be replaced by semicolons.

The exit status of the ir command is the exit status of the last command executed in any then clauSe or else clause. U no such command was executed, ir returns a aero exit status.

Note that an alternate notation for the test command uses brackets to enclose the expression being tested. For example, the previous example might have been written as follows:

Note that a space after the left bracket and one before the right bracket are essential in thisrorm of the syntax.

1-21

7.0.2 Using the case Statement

A multiple test conditional is provided by the case command. The basic format of the case statement is:

Im Dokument The Chapter (Seite 21-24)