Classnotes | UNIX01 | RecentChanges | Preferences As we mentionned before, one of the strengths of UNIX is that you can pipe the output of one application into the input of another. You can also redirect the output of an application into a file (which may assist in the analysis of that application's output).
Pipes
Pipes are a Unix mechanism which allow programs to be linked together according to a data dependency. Data in a pipe flows through a series of independent programs, or filters, along a one-way data path. Each filter in a pipe performs its own special transformation on the data before handing it to the next filter. This allows a number of small, special-purpose programs to be linked together to perform a more complicated task.
Filters are designed to be fairly small, smimple, special-purpose, and above all modular. This allows them to be used in a number of different situations without having to be modified. The sort program, for example, is a filter that performs one clearly defined operation: it sorts. No other program need be concerned with sorting data because sort does it so well. Other programs needing sorted data would simple create a pipe containing sort, and read from it.
Pipes are created by separating the various filter programs with the symbol "|" on the command line:
cat phone.list | sort | grep David
This pipeline has three filters: "cat phone.list," "sort," and "grep David." Data flows from left to right through the command line, passing through each filter in order. Data created by the first (the cat command), is read by the second (sort), acted upon, then passed to the third (grep). Any output that results at the end of the pipe will be written to standard output (whether that be your screen, or a file through some form of redirection). The effect of this example command is to produce a sorted list of entries in the "phone.list" file which contain the string "David".
The only requirement for pipes is that the filters which make up the pipeline must read input from a standard input source (STDIN), and write their output to a standard output (STDOUT). There can be any number of filters in a pipe, so long as all obey this rule.
Redirects
Unix normally accepts input from the keyboard (the characters you type) and sends its output (the characters it produces) to the screen. The keyboard and the screen are termed the standard input and standard output devices.
A command can also have standard input and standard output. For example, the first section of this how-to describes using cat to look at the contents of a file. cat takes its input from the filename following it, and normally sends its output to the standard output device, the screen. Some commands do not require input. The date command, for example, requires no input, but sends its output to the screen.
In Unix it is possible to "redirect" output -- to take what the system would have displayed on the screen and put it in a file instead. For example, when we issue the ls command a list of our files normally appears on the screen. If we use a redirection symbol (>), we can have this list redirected into a file:
$ ls > myfilelist
When you redirect output to a file that already exists, any previous contents are deleted before the command is completed. To prevent the accidental overwriting of files, first issue the command
$ set noclobber
If you then type
$ ls > myfilelist
where myfilelist already exists, you will see the response
myfilelist: File exists.
However, if you are certain you want to replace the contents of an existing file with redirected output, use the emphatic form of the redirection command:
$ ls >! myfilelist
It is possible to append the redirected output onto the end of an existing file, instead of replacing the contents, by using the append symbol (>>). The following command adds the date to the end of the file myfilelist, without removing its original contents:
$ date >> myfilelist
If you have issued the set noclobber command to prevent accidental overwriting, you must use the emphatic form:
$ date >>! myfilelist
Redirecting input
Just as you can redirect the output of a command, you can also redirect a command's input. Using the symbol < allows you to specify a file as input for a command which usually takes its input from the keyboard. For example, the command
$ cat < firstfile
is a valid way to display the contents of the file firstfile on the screen, although
$ cat firstfile
produces the same result. (In this second case, the filename is used as an argument for the cat command.)