< Day Day Up > |
13.18. PipesA 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
13.18.1 The here document and Redirecting InputThe 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
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
|
< Day Day Up > |