| Stream Redirection - Standard Error |
|
|
|
| Written by Tom Hirt | |||||||||||
| Tuesday, 24 February 2009 13:06 | |||||||||||
Page 4 of 5
Standard Error or STDERR STDERR can also be thought of as a output container, but different from STDOUT in that its contents are of errors from the running process. Also like STDOUT, STDERR usually defaults it's output to a screen buffer device like the console or a X terminal. Redirecting STDERR can also be useful for a variety of tasks; we will not go into all of them, but will highlight a couple examples to give you an idea. When running find on the root of your file-system as a non-privileged user, your console will often times become polluted with Permission denied errors making it very difficult to sift through the output distinguishing the search string from the errors:
We know that we don't have permissions to all of these folders, but for the ones we do, we would like find to search in them but less all the permission denied errors. We can do this by the use of the STDERR output modifier 2>. Using the STDERR output modifier, we can now choose to output all the errors to a text file (for further analysis) or we can just discard them altogether. In the first example, we'll send the errors to a text file:
As you can see, running find this time produced a much more clean output that only shows the exact hits of our search criteria. If we like, we can now go back and exam the contents of error_log.txt to see if there was anything of further interest. We could have also just as easily discarded the output as seen in our next example:
Here, we sent all the errors to the black hole, /dev/null. This can be useful when you know the command you are running will generate errors but you do not wish to see them. Appending STDERR Just like STDOUT, we can use the STDERR append modifier 2>> if we would like the output of our command to be appended to a file rather than overwriting the contents. For example:
From the above example, we see our first command ls completes with no output sent to the screen and STDERR sent to error_log.txt. The second command, find, sends the results of the search to our screen (STDOUT) and any errors (STDERR) to error_log.txt. Upon further inspection of error_log.txt, we see both commands threw errors but because we redirected STDERR, we never saw them until we inspected the contents of error_log.txt. |
|||||||||||
| Last Updated on Monday, 02 March 2009 15:57 |