Difference between revisions of "Seperating STDOUT and STDERR"

From assela Pathirana
Jump to navigationJump to search
 
Line 1: Line 1:
{{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>.}}
Consider the follwing situation: <nowiki>
Consider the follwing situation: <nowiki>
</nowiki><pre><nowiki>
</nowiki><pre><nowiki>
Line 4: Line 6:
</nowiki></pre> <tt>*</tt> indicates all the files in the current folder. Let's assume that you don't have a file named <tt> i-am-not-here-for-sure</tt> (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: <nowiki>
</nowiki></pre> <tt>*</tt> indicates all the files in the current folder. Let's assume that you don't have a file named <tt> i-am-not-here-for-sure</tt> (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: <nowiki>
</nowiki><pre><nowiki>
</nowiki><pre><nowiki>
good
$ ls *
Sample-rainfall    myfirstscript.bash  tmp
monthly-total.bash  myfirstscript.txt  tmp.bash
</nowiki></pre><nowiki>
</nowiki></pre><nowiki>
</nowiki><pre><nowiki>
</nowiki><pre><nowiki>
bad
$ ls  i-am-not-here-for-sure
ls: i-am-not-here-for-sure: No such file or directory
</nowiki></pre>
</nowiki></pre>
The two together: <nowiki>
</nowiki><pre><nowiki>
$ 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
</nowiki></pre>
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:
#First let's redirect the STDOUT to a file</nowiki><pre><nowiki>
$ ls * i-am-not-here-for-sure > out
ls: i-am-not-here-for-sure: No such file or directory
</nowiki></pre>only the error message (STDERR stream) shows on the terminal.
#Redirecting STDERR to a file</nowiki><pre><nowiki>
$ ls * i-am-not-here-for-sure 2> out
Sample-rainfall    myfirstscript.bash  out  tmp.bash
monthly-total.bash  myfirstscript.txt  tmp
</nowiki></pre> only the results (STDOUT stream) is on the terminal.
#Redirect STDERR to STDOUT and then write STDOUT to a file</nowiki><pre><nowiki>
$ ls * i-am-not-here-for-sure > out 2>&1
</nowiki></pre>No output!
Don't forget to also check the content of the file <tt>out</tt> each time.

Revision as of 12:57, 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.

Green info.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.