13.18. Pipes

 <  Day Day Up  >  

A pipe takes the output from the command on the left-hand side of the pipe symbol and sends it to the input of the command on the right-hand side of the pipe symbol. A pipeline can consist of more than one pipe.

The purpose of the commands in Example 13.87 is to count the number of people logged on ( who ), save the output of the command in a file ( tmp ), use the wc “l to count the number of lines in the tmp file ( wc “l ), and then remove the tmp file (i.e., find the number of people logged on).

Example 13.87.
 1    $  who > tmp  2    $  wc l tmp   4 tmp  3    $  rm tmp   # Using a pipe saves disk space and time.  4    $  who  wc l   4  5    $  du ..  sort n  sed n '$p'   1980   ..  6    $  (du /  sort -n  sed -n '$p') 2> /dev/null   1057747  /  

EXPLANATION

  1. The output of the who command is redirected to the tmp file.

  2. The wc “l command displays the number of lines in tmp .

  3. The tmp file is removed.

  4. With the pipe facility, you can perform all three of the preceding steps in one step. The output of the who command is sent to an anonymous kernel buffer; the wc “l command reads from the buffer and sends its output to the screen. See Figure 13.4.

    Figure 13.4. The pipe.


  5. The output of the du command, the number of disk blocks used per directory, starting in the parent directory ( .. ), is piped to the sort command and sorted numerically . It is then piped to the sed command, which prints the last line of the output it receives. See Figure 13.5.

    Figure 13.5. Multiple pipes (filter).

  6. The du command (starting in the root directory) will send error messages to stderr (the screen) if it is unable to get into a directory because the permissions have been turned off. When you put the whole command line in a set of parentheses, all the output is sent to the screen, and all the errors are directed to the UNIX/Linux bit bucket, /dev/null .

13.18.1 The here document and Redirecting Input

The here document is a special form of quoting. It accepts inline text for a program expecting input, such as mail , sort , or cat , until a user -defined terminator is reached. It is often used in shell scripts for creating menus . The command receiving the input is appended with a << symbol, followed by a user-defined word or symbol, and a newline. The next lines of text will be the lines of input to be sent to the command. The input is terminated when the user-defined word or symbol is then placed on a line by itself in the leftmost column (it cannot have spaces surrounding it). The word is used in place of Ctrl-D to stop the program from reading input.

If the terminator is preceded by the << “ operator, leading tabs, and only tabs, may precede the final terminator. The user-defined terminating word or symbol must match exactly from "here" to "here." The following examples illustrate the use of the here document at the command line to demonstrate the syntax. It is much more practical to use them in scripts.

Example 13.88.
  5    Hello there ellie  # FINISH on line 1.   The time is 19:42:12.   I can't wait to see you!!!  6    $ 

EXPLANATION

  1. The UNIX/Linux cat program will accept input until the word FINISH appears on a line by itself.

  2. A secondary prompt appears. The following text is input for the cat command. Variable substitution is performed within the here document .

  3. Command substitution, $(date +%T) , is performed within the here document . Could have also used the older form of command substitution: `date +T` .

  4. The user-defined terminator FINISH marks the end of input for the cat program. It cannot have any spaces before or after it and is on a line by itself.

  5. The output from the cat program is displayed.

  6. The shell prompt reappears.

Example 13.89.
 1    $  cat << DONE  >    Hello there      >        What's up?      >Bye now The time is `date`. 2    >  DONE   Hello there   What's up?   Bye now The time is Sun Feb 819:48:23 PST 2004.  $ 

EXPLANATION

  1. The cat program accepts input until DONE appears on a line by itself. The << “ operator allows the input and final terminator to be preceded by one or more tabs. Typing this example at the command line may cause problems with the Tab key; the example will work fine, if run from a script.

  2. The final matching terminator, DONE , is preceded by a tab. The output of the cat program is displayed on the screen.

 <  Day Day Up  >  


UNIX Shells by Example
UNIX Shells by Example (4th Edition)
ISBN: 013147572X
EAN: 2147483647
Year: 2004
Pages: 454
Authors: Ellie Quigley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net