4.3 Redirecting Standard Error

   

The stderr stream can be redirected in a similar fashion as stdin or stdout . Figure 4-4 shows what happens when we redirect the stderr.

Figure 4-4. Standard error redirection.

graphics/04fig04.gif

There is no special symbol for redirecting stderr. The same " > " symbol is used but with the number 2 attached in front of it. If you remember from previous pages, there are three file descriptors opened whenever a command is issued. These file descriptors are shown in Table 4-1.

Table 4-1. Standard File Descriptors
File Descriptor Number Description
Standard input
1 Standard output
2 Standard error

We use " 2> " for stderr redirection to tell the shell that we want to redirect the error messages instead of stdout (for which the file descriptor value is 0). Consider the following command.

 $  ll xyz  xyz not found. $ 

We tried to list a file with name xyz and the command result shows that this file does not exist. This is an error message of the ll command. Now see the following command.

 $  ll xyz >abc  xyz not found. $ 

We tried to redirect the output, but still the message is displayed on our screen. The redirection had no effect because we are trying to redirect stdout while the command is generating stderr messages. Now let us see what happens if we change " > " to " 2> ".

 $  ll xyz 2>abc  $ 

Now there is nothing displayed because the error message has been stored in a file with name abc . You can use the cat command to verify that the error message was indeed stored in the abc file.


   
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