Difference between revisions of "Seperating STDOUT and STDERR"

From assela Pathirana
Jump to navigationJump to search
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
{{Template:needs-cygwin-or-linux}}
{{Template:needs-cygwin-or-linux}}
{{Box|This example is written for [[wikipedia:Bash shell|Bash shell]] environment. It may not work in a differnt shell like <tt>csh</tt>.}}
{{Wbox|This example is written for [[wikipedia:Bash shell|Bash shell]] environment. It may not work in a differnt shell like <tt>csh</tt>.}}
Consider the follwing situation: <nowiki>
Consider the follwing situation: <nowiki>
</nowiki><pre><nowiki>
</nowiki><pre><nowiki>
Line 37: Line 37:


Don't forget to also check the content of the file <tt>out</tt> each time.
Don't forget to also check the content of the file <tt>out</tt> each time.
[[Category:Computing]]

Latest revision as of 13:00, 6 April 2006

Green info.gif

The commands on this page can be tried out on Cygwin installed in a windows PC or on any UNIX computer.

Red warning.gif

This example is written for Bash shell environment. It may not work in a differnt shell like csh.

Consider the follwing situation:

ls * i-am-not-here-for-sure

* indicates all the files in the current folder. Let's assume that you don't have a file named i-am-not-here-for-sure (that is unless you are crazy!), then the first argument of ls function generates a list of files and directories as output. Second one generates an error:

$ ls * Sample-rainfall myfirstscript.bash tmp monthly-total.bash myfirstscript.txt tmp.bash

$ ls i-am-not-here-for-sure ls: i-am-not-here-for-sure: No such file or directory

The two together:

$ ls * i-am-not-here-for-sure
ls: i-am-not-here-for-sure: No such file or directory
Sample-rainfall     myfirstscript.bash  out  tmp.bash
monthly-total.bash  myfirstscript.txt   tmp

Now, on the terminal, the error message and the output looks similar; but they came here by very differnt paths! Let's demonstrate this by selectively filtering the streams:

  1. First let's redirect the STDOUT to a file</nowiki>

$ ls * i-am-not-here-for-sure > out ls: i-am-not-here-for-sure: No such file or directory

only the error message (STDERR stream) shows on the terminal.

  1. Redirecting STDERR to a file</nowiki>

$ ls * i-am-not-here-for-sure 2> out Sample-rainfall myfirstscript.bash out tmp.bash monthly-total.bash myfirstscript.txt tmp

only the results (STDOUT stream) is on the terminal.

  1. Redirect STDERR to STDOUT and then write STDOUT to a file</nowiki>

$ ls * i-am-not-here-for-sure > out 2>&1

No output!

Don't forget to also check the content of the file out each time.