|


| 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 - Test Return Values
Continuing on from Monday's tip, here's an example in which we issue a command and want to report an error if the command failed. We test the special shell variable '$?': if it's zero then no error occurred, if its non-zero the command failed. Naturally, we want to send our error message to standard error, not standard out.
Here's how we achieve this in an example where we test the error status returned from the 'ls' command. To prove the message was sent to standard error and not standard out, we redirect standard out to the file 'out', but still see the error message on the screen.
$ ls zzz
ls: zzz: No such file or directory
$ if (($?!=0)); then echo "An error occurred" 1>&2; fi > out
An error occurred
Notice the use of '1>&2' to merge standard out into standard error, as in Monday's tip. Notice also that we took advantage of Bash's integer expressions in:
(($?!=0))
More conventionally we would have written:
$ if [ $? != 0 ]; then echo "An error occurred" 1>&2; fi >out
|





|
 |
|
 |
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. |
|