Scripting oddness (sh) - SOLVED (kind of)

David J. Weller-Fahy dave-lists-freebsd-questions at weller-fahy.com
Fri Mar 25 01:33:45 PST 2005


After some experimenting I believe that I've discovered how to fix the
problem, and the reason for it - I'd been thinking of the '&&' shortcut
as functionally identical to the if-then-fi construct.  That's obviously
(now ;) not the case.  If, as in the previous message, the following
'&&' shortcut is used:

#v+
[ -z "$RB_TEMP" ] && PKGLIST="$PKGLIST $PKG"
#v-

Then, if RB_TEMP is not of zero length, the exit status of that command
is one.  Since that command is the last performed in the
list_required_by function, the exit status of the function is one.
Because I used 'set -e' at the top of the script, any command (a
function being a complex command) that exits with a value of one halts
the execution of the script.  However, if we change that line to:

#v+
if [ -z "$RB_TEMP" ] ; then
	PKGLIST="$PKGLIST $PKG"
fi
#v-

Then, following the fi, the exit status is zero for both the command and
the function, and all following commands are executed.  The following
also works:

#v+
[ ! -z "$RB_TEMP" ] || PKGLIST="$PKGLIST $PKG"
#v-

Because the exit status of the first command on the line is zero if
RB_TEMP is not zero length.

So, there's a fix for it.  Figured I'd post this FTR, just in case
someone else has a lack of brain bytes similar to mine. ;]

Regards,
-- 
dave [ please don't CC me ]


More information about the freebsd-questions mailing list