Capture output of certain Unix commands

1 comment | 3I like it!
June 12, 2009, 12:37 PM —  Macworld.com — 

Today's hint will probably only appeal to those of you learning to use the Unix side of OS X. A while back, I was trying to capture the output of the Unix command httpd -t (which runs a syntax check on the Apache web server's configuration files) to the clipboard. Typically, you do this in OS X by sending the command's output through the pbcopy (pasteboard copy) command:

httpd -t | pbcopy

That should, in theory, put the output of the command on the clipboard. But in this case, it wasn't working. After much investigation, what I learned is that some Unix commands, httpd -t included, don't send their output to the Terminal in the usual way. The usual way in Unix is to send output to something called the "standard output," which for all intents in this discussion, means the Terminal window.

Instead, certain commands send their output to standard error (STDERROR, in Unix parlance). Although this text is also displayed in Terminal, you can't do anything with the output as you can if it were sent to standard output. (Unix wizards, please feel free to correct any of my mistakes in the above; it's meant to be a summary view of the situation.)

The solution is to reroute what gets sent to standard error to standard output, using this syntax:

httpd -t 2>&1

The 2 represents standard error, and the 1 represents standard output. The bit in the middle, >& is the usual Unix redirect command (the greater than sign), followed by the ampersand, which changes the redirect's output from a file (which is typical) to a file descriptor (in this case, the standard output).

I know that's a mouthful, and I'll admit that I don't fully understand it--but I know it works, at least in the bash shell that's been the default for a while now. Once the output is in standard output, it can just be fed through pbcopy as usual:

httpd -t 2>&1 | pbcopy

The end result, assuming there's nothing wrong with your Apache configuration files, will be the text Syntax OK on your clipboard. I know that ssh -v (verbose mode ssh) also sends its output to standard error, as does time, and I'm sure there are many more.

If you're a Unix expert and can clarify or expand on this in the comments, please feel free!

See more like this: unix-linux, Terminal, Mac OS

» posted by ITworld staff

Macworld.com

Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world

I like it!
Close

On Twitter now

mac os

Powered by Twitter
You are logged in | Sign out
Sign in and post to Twitter

What are you thinking?

Cancel Tweet sent

On Twitter now

Comments

Clarification

Actually, the '&' goes with the '1' to specify that the target is standard output (&1). You could just a easily redirect the standard error to a different file with
'2 > /somepath/somefile'.

This does not change your solution however, because the pipeline '|' will only feed the standard output to another program.
| reply
peer-to-peer

jfruh
Apple syncing patent can't come soon enough

pasmith
New Twitter features borrow from 3rd party clients

Esther Schindler
Open Source Changes the Software Acquisition Process

mikelgan
How to set up continuous podcast play on the new iTunes

David Strom
Five important Windows 7 mobility features

sjvn
Guard your Wi-Fi for your own sake                        

Sandra Henry-Stocker
Grepping on Whole Words

 

Sidekick: The Good News & the Bad News
Either way you look at it Microsoft Data Center management did not follow standards or best practices in this failure. In which case it makes me wonder more about the outsourcing of corporate data much less personal data.
- mburton325

Join the conversation here

The Daily Tip

The Daily TipQuick, practical advice for IT pros. Made fresh daily.

Hot tips:

Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.

Newsletters

Subscribe to ITWORLD TODAY and receive the latest IT news and analysis.

I would like to receive offers via email from ITworld partners.
By clicking submit you agree to the terms and conditions outlined in ITworld's privacy policy.
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace