11.14. 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 a command on the right-hand side of the pipe symbol. A pipeline can consist of more than one pipe.

Example 11.64.
 1   $  who > tmp  2   $  wc l tmp   4 tmp  3   $  rm tmp  4   $  who  wc -l   # Using the pipe  

EXPLANATION

The purpose of lines 1 through 3 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; that is, find the number of people logged on. The pipe performs the same task in one command.

  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 steps 1, 2, and 3 in one step. The output of the who command is sent to an anonymous kernel buffer (instead of to a temporary file that requires disk space); the wc “l command reads from the buffer and sends its output to the screen.

Example 11.65.
 1   $  ls  more   < lists (ls) all files one page at a time (more) >  2   $  du ~  sort n  sed n '$p'   72388  /home/jody/ellie  3   $  cat  lp   or   cat  lpr  

EXPLANATION

  1. The ls output is piped to the more command, which accepts input. Output is displayed one page at a time.

  2. The output of the du command (disk usage) is sorted numerically and piped to the sed command (stream editor), which displays only the last line ( $p ).

  3. The cat command reads from standard input; its output is piped to the line printer ( lp in SVR4 and lpr in BSD).

11.14.1 The here document and Redirecting Input

A here document captures in-line input for programs such as mail , sort , and cat . Input is placed between two words or symbols. The first word is preceded by a UNIX command and the << symbol. The next lines consist of the input to be received by the command. The last line consists of a second word that exactly matches the first word. This word is called the final terminator and marks the end of input. It is used in the same way Ctrl-D is used to terminate input. There can be no spaces surrounding the final terminator. If the first word is preceded by the << “ , leading tabs (and only tabs) may precede the final terminator. Normally, a here document is used in shell scripts, rather than interactively. A good use for a here document is to create a menu in a script.

FORMAT

 UNIX command << TERMINATOR      lines of input      input TERMINATOR 

Example 11.66.
 (The Command Line) 1   $  cat << FINISH  2  > Hello there $LOGNAME  3  > The time is $(date)   > I can't wait to see you!!!  4  > FINISH  5  Hello there ellie   The time is Sun May 30 19:42:16 PDT 2004   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. FINISH is called a user -defined terminator.

  2. Variable substitution is performed within the here document. The > is the Korn shell's secondary prompt.

  3. Command substitution is performed within the here document .

  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 11.67.
 (From the .profile File) 1   print "Select a terminal type" 2  cat << EOF   [1] sun   [2] ansi   [3] wyse50  3  EOF  4   read TERM     ... 

EXPLANATION

  1. The user is asked to select a terminal type.

  2. The menu will appear on the screen. This is a here document, meaning from here until the matching EOF on line 3 is reached, input will be given to the cat command. You could use a series of echo commands to get the same results, but visually, the here document is nicer.

  3. EOF is a user-defined terminator, marking the end of the here document. It must be at the left margin with no spaces surrounding it.

  4. The user input will be read in from the keyboard and assigned to TERM .

Example 11.68.
 (The Command Line) 1   $  cat << DONE  >Hello there     >What's up?     >Bye now The time is $(date). 2   >  DONE  3  Hello there   What's up?   Bye now The time is Sun May 30 19:48:23 PDT 2004.  

EXPLANATION

  1. The cat program accepts input until DONE appears on a line by itself. The << “ operator allows the final terminator to be preceded by one or more tabs. (The > is the shell's secondary prompt.)

  2. The final matching DONE terminator is preceded by a tab. From the first DONE on line 1 to the last DONE on this line, the text in between is sent as input to the cat command.

  3. 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