3333333333333333333333 This chapter introduces you to the operating system shells After completing
7.4 Command Entry Aids
7.4.1 Using Multiple Commands and Command Lists
7.4.1.2 Running Commands Conditionally
When you connect commands with two ampersand(&&)or pipe(||) operators, the shell runs the first command and then runs the remaining commands only under the following conditions:
&& The shell runs the next command only if the current command completes (a command indicates successful completion when it returns a value of zero).
|| The shell runs the next command only if the current command does not complete.
The syntax for the two ampersand (&&) operator follows: cmd1 && cmd2 && cmd3 && cmd4 && cmd5
Ifcmd1succeeds, the shell runscmd2. Ifcmd2succeeds, the shell runs cmd3, and on through the series until a command fails or the last command ends. (If any command fails, the shell stops executing the command line). The syntax for the pipe(||)operator follows:
cmd1 || cmd2
Ifcmd1fails, then the shell runs cmd2. Ifcmd1succeeds, the shell stops executing the command line.
For example, suppose that the commandmysort is a sorting program that creates a temporary file (mysort.tmp) in the course of its sorting process. When the sorting program finishes successfully, it cleans up after itself, deleting the temporary file. If, on the other hand, the program fails, it may neglect to clean up. To ensure deletion ofmysort.tmp, enter the following command line:
$ mysort || rm mysort.tmp $
The second command executes only if the first command fails.
7.4.2
Using Pipes and Filters
A pipe is a one-way connection between two related commands. One command writes its output to the pipe, and the other process reads its input from the pipe. When two or more commands are connected by the pipe ( | ) operator, they form a pipeline.
Figure 7-1 represents the flow of input and output through a pipeline. The output of the first command (cmd1) is the input for the second command (cmd2); the output of the second command is the input for the third command (cmd3).
Figure 7-1: Flow Through a Pipeline ZK−0537U−R Cmd2 (filter) Cmd3 (filter) Cmd1
A filter is a command that reads its standard input, transforms that input, and then writes the transformed input to standard output. Filters are typically used as intermediate commands in pipelines – that is, they are connected by a pipe ( | ) operator. For example, to cause thelscommand to list recursively the contents of all directories from the current directory to the bottom of the hierarchy, and then to display the results, enter the following command:
$ ls –R | pg
In this example, thepgcommand is the filter because it transforms the output from thels –R command and displays it one screen at a time. Certain commands that are not filters have a flag that causes them to act like filters. For example, thediff(compare files) command ordinarily compares two files and writes their differences to standard output. The usual format for difffollows:
difffile1 file2
However, if you use the dash (–) flag in place of one of the file names,diff reads standard input and compares it to the named file.
In the following pipeline,lswrites the contents of the current directory to standard output. Thediffcommand compares the output oflswith the contents of a file nameddirfile, and writes the differences to standard output one page at a time (with thepgcommand):
$ ls | diff – dirfile | pg
In the following example, another kind of filter program (grep) is used:
$ ls –l | grep r-x | wc –l 12
$
In this example, the following takes place:
• Thels –l command lists in long format the contents of the current directory.
• The output ofls –l becomes the standard input togrep r-x, a filter that searches for the files in its standard input for patterns with
permissions ofr-x, and writes all lines that contain the pattern to its standard output.
• The standard output ofgrep r-xbecomes the standard input towc –l, which displays the number of files matching thegrepcriteria in the standard input.
To get the same results without using a pipeline, you would have to do the following:
1. Direct the output ofls –l /userto a file. For example:
$ ls –l >file1
2. Usefile1 as input forgrep r-xand redirect the output ofgrepto another file. For example:
$ grep r-x file1 >file2
3. Use the output file ofgrepas input forwc –l. For example:
$ wc –l file2
As the preceding procedure demonstrates, using a pipeline is a much easier way to perform the same operations.
Each command in a pipeline runs as a separate process. Pipelines operate in one direction only (left to right), and all processes in a pipeline can run at the same time. A process pauses when it has no input to read or when the pipe to the next process is full.
7.4.3
Grouping Commands
The shell provides two ways to group commands, as shown in Table 7-3.
Table 7-3: Command Grouping Symbols
22222222222222222222222222222222222222222222222222222222222222222
Symbols Action
22222222222222222222222222222222222222222222222222222222222222222 (commands) The shell creates a subshell to run the groupedcommandsas
a separate process.
{commands} The shell runs the groupedcommandsas a unit. Braces can only be used in the Korn and Bourne shells.
22222222222222222222222222222222222222222222222222222222222222222 The following sections describe the command grouping symbols of Table 7-3 in greater detail.