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.
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 BreakUse of I/O Redirection
|
Top |