Piping Input and Output


In general, you can think of each Unix command (ls, cd, and so on) as an individual program that Unix executes. For example, if you type cat /etc/mold at the prompt, Unix will display the contents of mold in the /etc directory. Each program requires input (in this example, cat, the program, takes the contents of /etc/mold as input) and produces output (i.e., the displayed results).

Frequently, you'll want to run programs in sequence. For example, you could tell Unix to read your resume and then spell-check it. In doing this, you connect two commands together and have them run in sequence. This process, in which you connect the output of one program to the input of another, is called piping. Depending on what you want to do, you can pipe together as many commands as you wantwith the output of each command acting as the input of the next.

As Figure 1.7 shows, you pipe commands together using the pipe symbol, which is the | character. In the following example, we'll pipe the output of the ls command (which lists the contents of a directory) to the more command (which lets you read results one screen at a time). For details about more, see Viewing File Contents with more, later in this chapter.

Figure 1.7. To execute multiple commands in sequence, pipe them together using the pipe symbol (|).


To Pipe Commands:

  • ls | more

    Here, all you do is include a pipe symbol between the two commands, with a space on both sides of the pipe. This code produces a list of the files in the current directory, then pipes the results to more, which then lists the results one screen at a time (see Figure 1.7).

Tips

  • If you want to pipe more than two commands, you can. Just keep adding the commands (with a pipe symbol in between each, like | this) in the order you want them executed.

  • Remember that the output of each command is piped to the next command. So a piped command, such as ls | spell | sort, could list files within a directory, then spell-check the list, then sort the misspelled words and display them on screen. The filenames that are found in the system dictionary would not appear.

  • Venture to Chapter 15 to find out more about running a spell-checker and Chapter 6 to find out more about sorting.





Unix(c) Visual Quickstart Guide
UNIX, Third Edition
ISBN: 0321442458
EAN: 2147483647
Year: 2006
Pages: 251

Similar book on Amazon

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