Advanced pipeline usage



Last revision August 3, 2004

Table of Contents:

  1. The Unix file system
  2. The directory tree
  3. File ownership and permissions
  4. Files as units
  5. Examining file contents
  6. Other commands
  7. Standard Files and Data Pipes

Normally, only the standard output of the program on the left side of the pipe symbol is sent to the standard input of the program on the right side. In the C-shell, you can also have the standard error included with the standard output in the pipe by appending the ampersand symbol to the vertical bar symbol, for example,

ls -l |& grep ...

The tee program makes a data flow branch, with one copy of the data continuing to the standard output (or next pipe), while the other copy goes to a specified file. Example:

ps augx | grep 'gp111' | tee savefile | sort

In this case, the tee program sends one copy of the output of grep to the file savefile, while sending another copy on to the standard output, which is this case is piped to the sort program.

The cat program can be used to make an input data flow tee, that is, concatenating the input from a file with the standard input. This is done by piping the output of some program to a cat command that specifies both input filenames and the special filename -, which means insert the standard input (coming from the pipe or the terminal) into the concatenated output at this point. For example:

ls -l | cat header - trailer | lpr

would sandwich the output from ls -l between the contents of the two files header and trailer, which could be header and trailer lines that you could use to make a nicely formatted output from ls. The concatenated output is then piped to the line printer in this example, although it could be redirected to an output file.

Use the script command to save a copy of everything that appears on your screen into a file. Both the standard input that you type and the standard output and standard error streams that programs produce are captured by script into the file. Give the command:

script filename

Then everything that goes onto the screen (input or output) will also go into filename until you type a CTRL-D character or the command exit, which ends script and closes the file. Use this to get a record of a login session for debugging.

<--Previous Overview

Comments or Questions?