| Stream Redirection - Standard Output |
|
|
|
| Written by Tom Hirt | |||||||||||||
| Tuesday, 24 February 2009 13:06 | |||||||||||||
Page 3 of 5
Standard Output or STDOUT STDOUT can be thought of as the output container produced by a given process. Most programs will usually default their STDOUT to a screen buffer device such as the system console or a terminal like your X terminal. So for example, when running the command ls, the output that is produces is sent to our connected X terminal:
So in the example above, we see a simple directory listing of our working directory. The highlighted output from 'ls' was sent to STDOUT which happened to be our X terminal. We can now redirect the output of ls to a file by the use of our output modifier 1>. When using a output modifier, if the target file already exists, its contents will be overwritten. If the target does not exist, a new file will be created.
As you can see, the output which had previously be sent to our screen was redirected to a newly created file called file_list.txt. Like input redirection, we can drop the 1 from our syntax to simply the command.
Rerunning a command with the > will only overwrite the contents of what had previously been stored in the target file:
Appending STDOUT Appending of STDOUT to a target can be a very valuable tool to you for logging and other such utilities on a system. In the above example, with each iteration of the command echo, the targeted output's contents were replaced with the commands most recent output. You can append the output to STDOUT by the use of >> as the output modifier. For example:
In the above, we began by zeroing out the file using the overwrite output modifier, '>'. We then append, '>>' to the text "First Time:" followed by a directory listing, followed by the text "Second Time:" followed by a directory listing. I've highlighted the output to help illustrate this example. Although the above was a trivial example, in the following diagram, we've created a simple file monitoring program that makes use of the date command to log when a file trigger_file appears on the system. Lets take a look:
Still a very basic example, but you can now begin to see how powerful stream redirection can be. |
|||||||||||||
| Last Updated on Monday, 02 March 2009 15:57 |