| Stream Redirection - Combined Output |
|
|
|
| Written by Tom Hirt | |||||||||||||
| Tuesday, 24 February 2009 13:06 | |||||||||||||
Page 5 of 5
Combined Output The final part of our stream redirection discussion focuses on combined out. Leveraging what we have learned thus far, we may now combine both the STDOUT and STDERR streams into one file. A typical use case for this is system logging. We often see jobs scheduled through crond or at that redirect their output to STDOUT and/or STDERR for historical records of the transpired events. This can prove to be a very helpful tool when trying to understand why last night's batch process failed to run properly. In this example, we look at a basic syntax to combine both STDOUT and STDERR into a single file:
Notice the use of our append modifiers >> for both STDOUT and STDERR. Had we forgotten to use the append modifier, we would have lost the output from STDOUT. When combining the redirection for STDOUT and STDERR, STDOUT is always written first followed by STDERR. So depending on if you used the append or overwrite modifier for STDERR, your results will be quite different. I'll demonstrate with a couple examples. STDOUT = write, STDERR = append:
STDOUT = write, STDERR = write:
STDOUT = append, STDERR = write:
So what we have learned, in order to preserve the output of both STDOUT and STDERR in a combined redirection, the STDERR modifier must be set to append or the contents of STDOUT will be lost. Now although the above examples have helped us better understand the roll of the STDOUT and STDERR modifiers, we had to type a lot more than what is necessary. Lets lose some keystrokes and simplify our syntax. We'll start by using a combined output that will overwrite the target file's contents:
Our syntax from above with use of the single > followed by the file-name and then a 2>&1 tells our interpreter to combine both STDOUT and STDERR, and overwrite the contents of our target file with their output. For our final example, we combine both STDOUT and STDERR, but this time, appending to the contents of our target file:
As expected, the contents of the file were not lost as we used the append modifier, >>, to combine the output into the existing file. This concludes our article on Stream Redirection, we hope you found this helpful! |
|||||||||||||
| Last Updated on Monday, 02 March 2009 15:57 |