Page Banner

<<"Indistinguishable from Magick?" [Main Index] "Bloggers Safety Tip with Apache's mod_rewrite">>

Nifty UNIX tip of the day

03 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"; 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


my other sites:
- New York Running
- ISOGA Heavy Metal Band!
- Vacations in France!

- simon's scene
- mark's musings

- More Like it
- Worst Webhost ever?
- Against Bottled Water
- Recovering Disk Space in Windows
- Sun Ray Deployment at IS195
- Running Route Database - Update
- Pics from Mike's Bike Trip
- Central Park Moonlight Cycle
- Kristin Plater's Next Show...
- Kristin Plater Plays the Alphabet Lounge
- Your Chance to Influence US Energy Policy!
- Practical NY Energy Tips - PCs
- Missing the Point
- Practical NY Energy Tips - Lighting
- Making the Switch to Firefox
- Belize to Gutemala and Back Again
- Diving in Cancun
- Today...
- Peru and Belize!
- PC Fix Up Tips

-- powered by Greymatter -- email --