[Slackbuilds-users] Error checking in SlackBuilds

Robby Workman rworkman at slackbuilds.org
Tue Jan 30 04:25:41 UTC 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Grant wrote:
> On Mon, 29 Jan 2007 22:23:57 +0100, Niki Kovacs <contact at kikinovak.net> wrote:
> 
>> Hello everybody,
>>
>> Just checked out slackbuilds.org: thanks for that great idea! Just checking 
>> your guidelines, so I can possibly contribute the odd scripts.
>>
>> One detail. Quote from the submission guidelines:
>>
>> --8<---------------------------------
>> Build in error checking where possible; for instance, the following would 
>> cause the SlackBuild script to exit if the the decompress/extract operation 
>> fails:
>>
>>    tar -xzvf $PRGNAM.$VERSION.tar.gz || exit 1
>> --8<---------------------------------
>>
>> Wouldn't 'set -e' at the beginning of the script do the exact same thing?
> 
> Might do, but explicit script actions seem to be more popular than 
> global (obscure) settings?  Easier debug, if actions are local, 
> perhaps.  My own scripting I prefer explicit error action to some 
> global setting -- perhaps the global settings for things you might 
> turn off, for example set -x for (sledgehammer) debugging.


We've had quite a bit of internal discussion about using "set -e"
versus "$COMMAND || exit 1" in stuff, and we pretty much decided
that either would be fine with us.  The biggest issue we (and anyone
else in our situation) run into with error checking is that subshells
have to be handled differently.  For example:

#!/bin/sh
set -e
cat file_that_exists
echo Still going...
cat file_that_does_not_exist
echo This should never be seen

That script would exit on line 5 because the file doesn't exist.
However, if that operation is launched inside a subshell:

( cat file_that_does_not_exist || exit 1 )

then the script will continue to run, even though the operation fails
and the 'exit 1' occurs -- it's the *subshell* that exits, not the
calling shell.  This happens regardless of whether we explicitly
"else" the "exit 1" as above or "set -e" is specified earlier in the
script.  In cases where subshells are wanted/needed, and error
checking is also desired, we could do this:

( do_something || exit 1 )
if [ ! $? =  0 ]; then
  exit 1
fi

There's almost certainly a more elegant way to do it, but that gets
the idea across, I think.

- --

http://slackbuilds.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFFvsjF7QPvQNDlLwQRAjg1AKCaiQe+hz/NaawCrzQBeJbTATbvvACdEsGb
zSQmdf51r3zL6U5bEgdtwdQ=
=PY2k
-----END PGP SIGNATURE-----



More information about the Slackbuilds-users mailing list