Chapter 4. InputOutput Redirection and Pipes

   

Chapter 4. Input/Output Redirection and Pipes

Chapter Syllabus

4.1 Redirecting Standard Output

4.2 Redirecting Standard Input

4.3 Redirecting Standard Error

4.4 Redirecting Standard Input, Output, and Error Simultaneously

4.5 Pipes and How They Are Used

4.6 The T-Junction

Most UNIX commands are designed to take simple text ( alphanumeric ) data and punctuation as input. Usually, the output is also of simple text. Whenever you start a UNIX command, it opens three standard data streams: standard input (stdin), standard output ( stdout ), and standard error (stderr). Every UNIX command takes input data from stdin and sends its normal output to stdout and error messages to stderr. These data streams are often called standard input/output. UNIX associates numbers known as file descriptors with all open files. File descriptor 0 is used with standard input, 1 with standard output, and 2 with standard error.

Standard input, usually the user keyboard, is normally the place where a program reads its input from. Standard output, usually your terminal screen, is where the results of a command or program are displayed. In normal cases, standard error messages are also displayed on the terminal screen, but it is always possible to separate stdout from stderr. The UNIX shell can redirect any of these streams to a file, a device, or some other command, as required by the user . We call this process I/O redirection . You studied one example of output redirection in Chapter 2, when you created a new file with the cat command. In its normal use, the cat command reads from the keyboard (stdin) and writes to the terminal screen (stdout). We used the " > " symbol to redirect output from the stdout to a file. Similarly, when we displayed the contents of a file with the cat command, we redirected input to the cat command from the keyboard (stdin) to the file. Figure 4-1 shows the standard location of input, output, and error for any UNIX command or program.

Figure 4-1. Location of standard I/O.

graphics/04fig01.gif

Another useful feature of UNIX is the pipe , with which we can send output of one command to the input of another command. This is often used to process and format data produced by a command and make it more understandable. Many commands are used as filters in UNIX, which take input from a command, filter the required data, and throw away the garbage. For example, the cat /etc/passwd command displays the contents of the password file, but using a filter we can extract only login names of the system users.

Sometimes UNIX is also called a file-based operating system, meaning that any type of input or output device can be considered as being a file. All of the devices connected to a system are controlled through device driver files. When you want to print something, just direct it to the printer device file. If you want to send something to the terminal display, send it to the display device file. I/O redirection and pipes are considered very powerful features of UNIX, as any combination of commands can be used to get the desired result.

In this chapter, you will learn how to redirect any type of input, output, and error to another location. You can also redirect all of these at the same time. You will also learn the uses of pipes and tees to filter and redirect data to multiple locations.


   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

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