Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Insidious exit status bugs



The following really messed me up, trying to write a script that will work
whether sh is bash or zsh.  Here's zsh 3.0.5 (3.1.4 is the same):

zagzig% echo yyy | fgrep -q xxx && echo ok
zagzig% echo yyy | fgrep -q `echo xxx` && echo ok
ok

It appears that the exit status of `echo xxx` is masking the exit status of
`fgrep -q`, but I'm not certain.  I had to do this workaround to get both
shells to behave the same:

xxx=`echo xxx`
echo yyy | fgrep -q $xxx && echo ok

Bash, of course, has an equally annoying bug:

( echo xxx | while read; do exit 0; done; exit 1; )
echo $?

Execute the above lines in bash and you'll see that $? = 1 whereas in zsh
the $? = 0.  Since I'd like to have the script exit nonzero only if the
read reaches end-of-file, I was forced into this sort of foolishness:

( echo xxx | while read; do exit 0; done; exit 1; )
ok=$?

compute some things |
while read some things
do
    [ "$some" != "$things" ] && continue
    exit $ok
done || exit 0
exit 1

In zsh, the "exit $ok" terminates the whole script with $? = 0.  In bash,
it only terminates the loop, triggering the || exit 0 clause.  In either
case, if the read gets EOF, the loop finishes sucessfully and the exit 1
then follows it.

Oof.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com



Messages sorted by: Reverse Date, Date, Thread, Author