• No se han encontrado resultados

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.

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.

4.1 Redirecting Standard Output

Redirection of stdout is controlled by ">" the greater-than symbol. The process of redirecting output is shown in Figure 4-2. The command takes input from the keyboard but sends its output to a file on disk.

Figure 4-2. Standard output redirection.

Note that error messages still go to the terminal screen. To demonstrate the process of output redirection, we can use the same example of Chapter 2, where we displayed contents of a file as follows.

$ cat newfile This is first line. This is the second line. This is third and last line. $

To redirect the output of the cat command we use the following step. $ cat newfile > file1

$

Now the cat command displayed nothing, as the output of the command is redirected to a file. If we check the contents of file file1, it will contain the same text as newfile (the output of the cat command).

Note

This is another way of copying text files. As you go through the book, you will find how versatile the UNIX commands are and how many different ways these commands can be used. Until now, you have used the cat command to create a new file, display contents of a file, and copy a text file using redirection. The same command is used for other purposes as well, and you will learn more uses of the cat command later in this chapter.

As another example, consider the who command. We redirected its output to a file with the name whofile. We can verify the contents of whofile with the more or cat command.

$ who > whofile $ cat whofile

operator pts/ta Aug 30 16:05 boota pts/tb Aug 30 15:59 john pts/tc Aug 30 14:34 $

Note

If a file with the name file1 already exists, it will be overwritten by using the above command without any warning.

Joining Two or More Files

Two or more files can be joined into a single file by the use of the cat command and redirecting output to a file. Let us suppose there are three files in your home directory, with the names file1, file2, and file3. If you use the cat command with file1 and file2 as its arguments, it will show you the contents of file1 and file2, respectively. What if we use the cat * command? It will display the contents of all files in the directory. Now, by simply redirecting the output to another file, the command will concatenate all of these files. $ cat file1 file2 >file4

$

This command created file4, which contains the contents of both file1 and file2. The following command creates file5, containing all files in the directory.

$ cat * >file5 $

This is the another use of the cat command is for joining two or more files.

Appending to a File

In the case of output redirection with the ">" symbol, the file to which we redirect the output of a command is overwritten. It means that the previous contents of the file are destroyed. We can use the double redirection symbol ">>" to keep the previous contents of the file. In such a situation, the output of a command is appended to the file. Consider the following example.

$ cat file1 >>file2 $

This command means that file2 still contains the old contents of file2. In addition to this, the contents of file1 are added to the end of file2. If file2 does not exist, it is created. This is a very useful feature and is used in many situations. For example, if we want to check how many users are logged in every hour, we can ask UNIX to run date and who commands every hour and redirect (append) the output of both of these commands to a log file. The date command will append the current date and time and the who command will append a list of users. Later on we can view this log file to get the desired information.

Redirecting Standard Output to Devices

In addition to redirecting output of a command to a file, you can also redirect it to any device, as UNIX treats all devices as files. Just as an example, the device file of the console is /dev/console. If you want to send the contents of a file to the console, you can use the following command.

$ cat file1 >/dev/console $

The file will be displayed on the console screen. Similarly, if you know the device file name of another terminal, you can send a file to the monitor of that terminal.

Note

Many times, systems administrators use this procedure to diagnose a faulty terminal. If you don't get a login prompt of an attached terminal, try to send a file to that terminal with the above-mentioned procedure to ensure that the cabling is not faulty. If the file is displayed on the terminal screen, you have assurance that there is no hardware fault and that something is missing in the configuration of that terminal.

Sometimes you can use the same redirection method to print simple text files, if the printer is directly connected to the HP-UX machine and you know the device name for the printer.

When redirecting output, keep in mind that sterr is not redirected automatically with the output. If the command you issue generates an error message, it will still be displayed on your own terminal screen. 4.2 Redirecting Standard Input

UNIX commands can send output anywhere when using output redirection, and they can also get input from places other than the keyboard. Figure 4-3 shows I/O locations in the case of stdin redirection.

Figure 4-3. Standard input redirection.

We use the "less-than" symbol (<) for input redirection. Say that you have already created a text file with name myfile. You want to send this text file to a user jane through electronic mail. The easiest way to do

this is to ask the mail program to get its input from this file instead of from the keyboard. The process of doing this is as follows.

$ mail jane <myfile $

The mail program sends an email message to user jane on the current system consisting of the contents of myfile. This is a more convenient way to type messages when you need time to compose them. You just create a file, and when you are satisfied with what you have typed, send it through email.