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.

graphics/04fig02.gif

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  $ 

Note

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.


   
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