46.9 If Command Doesn't Return a Status, Test the Error MessagesUNIX commands should return a zero exit status ( 44.7 ) if they succeed or a non-zero status if they fail. Not all commands do. For example, here's a log I found of a test I did back in 1985 to the 4.3BSD version of touch ( 21.7 ) :
$ Because the status was always zero, as if the command had succeeded, my shell scripts couldn't test the exit status for failure. A workaround is to make your own shell script version of touch (maybe call it mytouch )-or to put code like this into a shell script where you need to run touch : [1]
That code handles many of the error exits; other errors get an exit status of 10. You could simplify the code to return a status of 1 on any error. On the other hand, it would be easy to expand that code to cover any possible (or interesting) error output. Article 27.19 shows how to get a list of many of the possible error messages; you can also see the errors by making some typical mistakes like unreadable or unwritable filenames. That code won't work everywhere. The command you're running might write text to standard output that you don't want mixed together with error messages (on the standard error). Or, worse, some vendors' versions of some commands write errors to the standard output! Still, this technique should give you a good start on working around badly behaved UNIX commands. - |
|