[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: set -e, but test return not honoured _conditionally_

From: Pierre Gaston
Subject: Re: set -e, but test return not honoured _conditionally_
Date: Tue, 22 Feb 2011 16:06:57 +0200

On Tue, Feb 22, 2011 at 3:46 PM, Greg Wooledge <address@hidden> wrote:

> On Fri, Feb 18, 2011 at 04:36:06PM +0100, Steffen Daode Nurpmeso wrote:
> > I am not a sophisticated shell programmer, but i really think this
> > time it's a shell fault.
> You think *what* is the shell's fault?
> > You may invoke the code snippet via 'script test1 test3' or so.
> >
> >       #!/bin/sh
> >       set -e
> >
> >       _t() {
> >           echo "Entry _t for $CURR"
> >           test "$PARAMS" != "${PARAMS/$CURR/}" && { return; }
> >           # Uncomment the next line and the script won't fail!
> >           #echo "Exit _t for $CURR"
> >       }
> >
> >       PARAMS="$@"
> >
> >       CURR='test1'   _t
> >       CURR='test2'   _t
> >       CURR='test3'   _t
> Setting aside for the moment what you are attmepting to do here (which
> looks rather unorthodox), I don't see what your complaint is.  You
> asked for "set -e", meaning for the shell to exit any time a simple
> command fails.  Then you called three simple commands in a row, each
> one named "_t".  If any of them fails, the shell is supposed to exit.
> And it does so, yes?  Is that what you are complaining about?
> Are you confused about what your function is doing?  It is returning
> success or failure based on what's in the variables PARAMS and CURR.
> When it fails, the exit status tells bash to abort, because you asked
> bash to do so.
> http://mywiki.wooledge.org/BashFAQ/035 - How can I handle command-line
> arguments (options) to my script easily?
> http://mywiki.wooledge.org/BashFAQ/105 -- Why doesn't set -e (set -o
> errexit) do what I expected?
More explanations on your example.

This line:

test "$PARAMS" != "${PARAMS/$CURR/}" && { return; }

Never cause the shell to exit because it's  complex command
However it causes the shell function to exit with non 0 if the test fails.

So your call to _t returns with 1 and the shell exits because _t is a simple

When you add echo, it is executed because the complex command doesn't cause
the shell to exit and since echo succeeds,
it  causes the function to return 0 and your call to _t doesn't exit the
shell anymore.

PS: maybe you wanted "|| return" so that it always return 0?

reply via email to

[Prev in Thread] Current Thread [Next in Thread]