Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stderror question

Status
Not open for further replies.

hokky

Technical User
Nov 9, 2006
170
AU
Hi guys,

I just wanna know the difference between these 3 pls :

echo >&2 "program is running"
echo "program is running" >&2

and normal echo :
echo "program is running"

what the >&2 exactly means ? I read that to put to standard error output.

But can anyone can explain exactly how it works ? and how do I check it.

Thanks guys
 
Hi

[tt]echo >&2 "program is running"[/tt] - redirects the output from stdout to stderr ( unusual, never saw it used in practice )
[tt]echo "program is running" >&2[/tt] - redirects the output from stdout to stderr
[tt]echo "program is running"[/tt] - nothing

In Unix and Unix-like operating systems there are three standard I/O devices, with fixed file handles :
0 - stdin ( standard input )
1 - stdout ( standard output )
2 - stderr ( standard error )

By default the programs read from stdin and write to stdout. But this can be changed.

The [tt]>&2[/tt] means redirect the output stream sent to file handle 1 - stdout ( default, so not mentioned in fron tof the greater-then ( > ) sign ) to file handle 2 - stderr ( the ampersand ( & ) says that a file handle follows, because by default there should be file name ).

But of course, the system can handle more file handles then three, so neither the redirecting is not limited to those three standard values.

I suppose that you use [tt]ksh[/tt], but I can suggest only [tt]bash[/tt] document, the Advanced Bash Scripting. But as far as I know, the redirections works the same way in both shells.


Feherke.
 
So,

what's the result difference between :
echo "program is running" >&2
echo "program is running"

because the output exactly the same, which exact file is the "program is running" redirected ?

what is the file "handle 2-stderr", what's the default filename ?

Thanks man,

 
Hi,

Every command you run under shell has at least 3 file descriptors :
0 : is the filedescriptor of the standrd input ( by default keybord )
1 : is the descriptor of the normal output file ( by default screen )
2 : is the descriptor of the error output ( by default the same as normal output "screen" )

&0 : is the reference of the standard input
&1 : the reference of normal input
&2 : the reference of error output

When you write something like this :
Code:
$ls /tmp/inexist.file
/tmp/inexist.file not found
If you don't want errors to be displayed on the standard output, you can redirect them to a file on disk

Code:
$ ls /tmp/inexist.file 2>/tmp/z.log
$cat /tmp/z.log
/tmp/inexist.file not found

If you don't want the standard output to be displayed on the screeen, you can write something like this

Code:
ls -lR /usr 1>/tmp/usr.list

and, if you want also the standard error to be apended to the result of the standard output :
Code:
ls -Rl /usr 1>/tmp/usr/list 2>&1
2>&1 means standard error redirected to the same thing of standard output

Is it clear now ?



 
Hi

[tt]echo "program is running" >&2[/tt] - outputs to stderr
[tt]echo "program is running"[/tt] - outputs to stdout

If you want to see the difference, you could for example pipe the output to [tt]sed[/tt] and alter it :
Code:
[gray]# sed modifies the text because it receives it[/gray]
[blue]master #[/blue] echo "program is running" | sed 's/^/got this line : /'
got this line : program is running

[gray]# the text does not reach sed because passes on other device[/gray]
[blue]master #[/blue] echo "program is running" >&2 | sed 's/^/got this line : /'
program is running
hokky said:
because the output exactly the same
Looks identical because by default both stdout and stderr are displayed on the screen. But their ways are different.
hokky said:
which exact file is the "program is running" redirected ?

what is the file "handle 2-stderr", what's the default filename ?
As you asked about files, they are /dev/stdin, /dev/stdout and /dev/stderr ( at least on Linux ). But they are not regular files, but character devices.

Feherke.
 
Thanks guys,

I do understand about 2>&1

but I don't understand about >&2

When is the best do you reckon to use >&2, in what condition ?

Could you explain please ?
 
>&2 is a shorthand for 1>&2

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi,

assume we don't need any error messages, so we can redirect standard error like in this fiction script :
Code:
...
#I don't need to see errors
exec 2>/dev/null  
....
grep "some thing" /some/path/to/file 1>&2
if [ $? -eq 0 ]
then
   echo /some/path/to/file contains "some thing" 
   exit
fi
...

In the grep command, I don't need to see the result, I only want to be sure the file contains some pattern or not.
 
hokky,

Just as programs like grep output errors to stderr and regular output to stdout, you might want your script to do the same.

Generally this is so that errors will still print to screen by default when stdout has been redirected.

Given script_sample:
Code:
...
echo "I really want this to be seen" >&2
---

will get the user's attention even if they redirect stdout:
Code:
/# script_sample > /dev/null
I really want this to be seen
/#

Of course, the user can always redirect stderr, but then they are choosing explicitly not to see errors.

- Rod






IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

Wish you could view posts with a fixed font? Got Firefox & Greasemonkey? Give yourself the option.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top