Owner's manual

In the following example, the shell runs each command only if the previous command has executed
successfully:
$ cmd1 && cmd2 && cmd3 && cmd4 && cmd5
If cmd1 succeeds, the shell runs cmd2. If cmd2 succeeds, the shell runs cmd3, and so on through
the series until a command fails or the last command has been run. (If any command fails, the shell
stops executing the command line).
In the following example, the shell runs each command only if the previous command has failed:
$ cmd1 || cmd2
If cmd1 fails, the shell runs cmd2. If cmd1 succeeds, the shell stops executing the command line.
For example, suppose that the command mysort runs a sorting program that creates a temporary
file (mysort.tmp) during its sorting process. When mysort finishes successfully, it deletes the
mysort.tmp temporary file. If, on the other hand, mysort fails, it may neglect to delete
mysort.tmp. To ensure deletion of mysort.tmp, use the following command line:
$ mysort || rm mysort.tmp
The second command, which removes mysort.tmp, executes only if mysort fails.
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 command reads its input from the pipe. When two or more commands
are connected by the | (pipe) operator, they form a pipeline. The output of the first command is
the input for the second command; the output of the second command is the input for the third
command, and so on.
A filter is a command that reads its standard input file, transforms that input, and then writes the
transformed input to the standard output file. Filters are typically used as intermediate commands
in pipelines; that is, they are connected by a | (pipe) operator. For example,
ls -R | sort
causes the ls command to list recursively (-R) the contents of all directories from the current
directory to the bottom of the hierarchy; the sort command sorts the results alphabetically and
displays the alphabetized list on the screen. The sort command is a filter because it alphabetizes
the list created by the ls -R command and displays the modified (alphabetized) list on the screen.
Certain commands that are not filters have a flag that causes them to act like filters. For example,
the diff (compare files) command ordinarily compares two files and writes their differences to
the standard output file. The usual format for diff follows:
diff file1 file2
However, if you use the - (dash) flag in place of one of the filenames, diff reads the standard
input file and compares it to the named file.
In the following pipeline, ls writes the contents of the current directory to the standard output file.
The diff command compares the output of ls with the contents of a file named dirfile and
then writes the differences to the standard output file (with the more command):
$ ls | diff - dirfile | more
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:
1. The ls -l command lists in long format the contents of the current directory.
2. The output of ls -l becomes the input to grep r-x, which acts as a filter that searches the
files in its standard input file for patterns with permissions of r-x, and writes all lines that
contain the pattern to its standard output file.
36 The OSS Shell