4.4 Redirecting Standard Input, Output, and Error Simultaneously

   

As you become more and more accustomed to HP-UX, you will often need to run unattended programs that execute at specific times, for example, at midnight. You need the program to take input from some files, such as system log files, and send its output to some other file. You also need to know if some error occurred during the execution of such programs. You can then look over the results of the program and any errors at any convenient time. This is the case where you redirect all of the three types of standard I/O to files. See Figure 4-5, showing where data streams come and go in such a case.

Figure 4-5. Redirection of standard input, output, and error.

graphics/04fig05.gif

We shall demonstrate the use of all of the three redirections with the sort command. Let us suppose we have a file with name unsorted with the following four lines in it.

 $  cat unsorted  This is number 1 This is number 5 This is number 3 This is number 2 $ 

We can use the sort command to arrange (sort) these lines. When we use the sort command with input redirection to this file, this result appears.

 $  sort < unsorted  This is number 1 This is number 2 This is number 3 This is number 5 $ 

Now we can redirect output of the command to a file named sorted and the error to a file named error with the following command.

 $  sort <unsorted >sorted 2>error  $ 

Does this seem complicated to you? Indeed it is not. You can even change the order in which input, output, and error files appear.

Study Break

Use of I/O Redirection

As you have seen, I/O redirection is an important tool for a UNIX user. Until now you have studied all types of I/O redirection. As you have seen, you can use one or all types of redirection with a command. The I/O redirection feature is used by system administrators extensively in scripts used for system maintenance purposes. Most of the time, these scripts are time scheduled and run without any user interaction. It is very useful to record output of these scripts to diagnose any problem occurring during execution. You will see in the next section that the pipe is another very important tool, which, when used with I/O redirection, can filter useful data from system log files.

This is the time to practice with I/O redirection. Use the date command and redirect its output to a file named logfile . Use the who command and append its output to the same file. Wait for five minutes and again use the date and who commands and append their output to logfile . Now use the cat command to display logfile . You will see that it contains a line for time and date and then a list of users who were logged in at that time. You can use this technique with the UNIX scheduler ( cron ) to create a log for a whole day and study it later.


   
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