trapping errors in test scripts

Cristian Ionescu-Idbohrn cristian.ionescu-idbohrn at axis.com
Wed May 7 10:30:50 PDT 2008


Please consider these 2 examples scripts (tested with bash, dash and zsh):

,---- [ x1 ]
| #/bin/sh
|
| set -e
|
| status=0
| do-this
| test $? -eq 0 || status=1
|
| if [ $status -eq 0 ]; then
| 	echo "### show success" >&2
| else
| 	echo "*** show some error message" >&2
| fi
|
| exit 0
`----

The result from running the above:

,----
| # /tmp/x
| /tmp/x: line 6: do-this: command not found
`----

And this:

,---- [ x2 ]
| #/bin/sh
|
| set -e
|
| status=0
| do-this || status=1
|
| if [ $status -eq 0 ]; then
| 	echo "### show success" >&2
| else
| 	echo "*** show some error message" >&2
| fi
|
| exit 0
`----

The result from running the above:

,----
| # /tmp/y
| /tmp/y: line 6: do-this: command not found
| *** show some error message
`----

Now, it seems to me that [ x2 ] behaves more like expected, which implies
this method of trapping errors:

,----
| status=0
| do-this || status=1
`----

is more appropriate than this one:

,----
| status=0
| do-this
| test $? -eq 0 || status=1
`----

when:

,----
| set -e
`----

is used, but also when it isn't.

This also implies that scripts using the [ x1 ] method will be more error
prone.

Thoughts?


Cheers,

-- 
Cristian


More information about the busybox mailing list