<<"Indistinguishable from Magick?" [Main Index] "Bloggers Safety Tip with Apache's mod_rewrite">>
Nifty UNIX tip of the day03 December 2003
I was writing a quick script and wanted to process the standard error (stderr) output of a program and ignore the standard output (stdout).
My goal was to check a Webserver was successfully serving a page and returning a successfulll HTTP code (Code 200). I was using the wget tool. Wget has a '-S' option for outputting the HTTP headers as a page is loaded, but these are sent to stderr not stdout. Everyone (even my mother) knows you can redirect stdout with '>' or pipe (|) it onto the next program. Most people know that you can redirect stderr to a file using '2>'. But by itsself, that doesn't let you pipe it to the next command in the chain. To do that, you need to redirect the stderr to stdout. In bash, this is done by '2>&1'. The you can pipe onto the next command. eg/
[djg@caperdu]$ wget -S localhost -O /dev/null 2>&1 | grep -c "1 HTTP"
(-O means send the downloaded web page to /dev/null, and grep's -c option is to count the number of occurances)
NB if you are going to redirect stdout, you have to do that before redirecting stderr. eg/
wget -S localhost > out 2>&1
NB II: Remember you will then be getting both the stdout and stderr in your output.
This is in the bash shell
Replies: 1 Comment
You can also try "wget -o /dev/stdout -O /dev/stdout
http://www.corunet.com" without the need of pipes. It works flawlesly.
I found it while aplying your advice and, for me, is even more
David Pardo said @ 12/28/2004 01:39 PM EST