|


| NAVIGATION |
|
Home |
|
Store |
|
|
| INSIDE MAC |
|
Television Shows |
|
Broadcast Shows |
|
Daily News Shows |
|
Special Shows |
|
|
| EVENTS |
|
|
|
|
|
|
|
|
| DAILY TIPS |
|
Design |
|
Mac OS X |
|
Mac OS X UNIX |
|
|
| COMMUNITY |
|
Forums |
|
Surveys |
|
|
| NEWS |
|
Current |
|
Press |
|
Archive |
|
|
| FEATURES |
|
Editorial |
|
Dr. Mac |
|
Reviews |
|
Reader Reports |
|
|
| RESOURCES |
|
FAQ |
|
Documentation |
|
Learning Center |
|
MAN pages |
|
Glossary |
|
Tutorials |
|
Tips |
|
Links |
|
|
|

|
|
|
OSXFAQ Mac OS X UNIX Tip-of-the-Day
Redirection - Echo to Standard Error
This week we'll look at some handy redirection tricks. They're of most use in shell scripts, but can be used directly on the command line too.
First off, here's how a script might send an error message to standard error instead of standard out. Normally, when your script writes any type of message using a command such as 'echo', output is sent to standard out.
Why would we want to write to standard error instead? Traditionally, Unix commands such as 'ls' and 'mount' write output to standard out and error messages to standard error. This convention ensures that if we were to pipe the command's output to another command (or redirect it to a file) error messages would still displayed on the terminal screen and would not get mixed up with the 'normal' output. We want our scripts to honour this convention.
As an example, let's write the error message "Can't open the file." to standard error.
$ cat example
#!/bin/bash
# some processing here...
echo "Can't open the file." 1>&2
The key is this after the echo command:
1>&2
which merges standard out (stream 1) into standard error (stream 2), hence causing 'echo' to write to standard error.
We run it and see the error message:
$ ./example
Can't open the file.
$
To prove the message went to standard error and not standard out, we redirect standard out to the file 'out' and standard error to the file 'err', displaying the contents of each file:
$ ./example >out 2>err
$ cat out
$ cat err
Can't open the file.
$
|





|
 |
|
 |
Copyright © 2000-2008 Inside Mac Media, Inc. All rights reserved. |
Apple assumes no responsibility with regard to the selection, performance, or use of the products or services. All understandings, agreements, or warranties, if any, take place directly between the vendors and prospective users. |
| Apple, the Apple logo, Mac, PowerMac G4, PowerMac G5, Xserve, Xserve RAID, PowerBook, iBook, Airport, AirPort Extreme, iMac, eMac, iLife, iMovie, iCal, iPhoto, iTunes, QuickTime, FireWire, iPod, iSight, AppleWorks, Macintosh, Jaguar, Panther, Mac OS, Mac OS X and Mac OS X Server are trademarks of Apple Computer, Inc. |
|