Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 18 Jul 2009 03:48:35 +0100
From:      Adrian Wontroba <aw1@stade.co.uk>
To:        Brett Glass <brett@lariat.net>
Cc:        chat@freebsd.org
Subject:   Re: Bourne shell short-circuit operators improperly documented
Message-ID:  <20090718024835.GB8379@steerpike.hanley.stade.co.uk>
In-Reply-To: <200907180121.TAA16416@lariat.net>
References:  <200907172257.QAA15292@lariat.net> <20090718000116.GA8379@steerpike.hanley.stade.co.uk> <200907180121.TAA16416@lariat.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, Jul 17, 2009 at 07:21:14PM -0600, Brett Glass wrote:
> At 06:01 PM 7/17/2009, Adrian Wontroba wrote:
> >No it is succinctly correct but confusing (the UNIX way?). These
> >operators work on exit codes where 0 = success = true and and !0 =
> >failure = false.
> As I understand it, when it comes to UNIX result codes, 0 doesn't 
> really mean "true" -- it means "no error." (In other words, it 
> means "false.") Whereas any nonzero value means there was an error 
> (and indicates what kind). In other words, it means that it's 
> "true" that there was an error.
> So, the semantics of the operators are supposed to be that "false" 
> is "true?" Aaargh!

I think mentioning true and false confuses (even more). In this
context 0 is success, anything else is failure. That said, the sh man
page earlier refutes your 0 means false with:

Each command has an exit status that can influence the behavior of other
shell commands.  The paradigm is that a command exits with zero for nor-
mal or success, and non-zero for failure, error, or a false indication.
                    ^^^^^^^^                          ^^^^^

> No wonder I don't use short circuit operators much. When zero 
> equals one, it gets rather confusing.

I agree that they can be confusing. Forget the 0 and 1, just think of
success and failure.

> It's also confusing that they are called "AND" and "OR" operators 

I agree with that too. As Jeremy wrote, "conditional execution" would
have been a better description.

> (and look like the short-circuit AND and OR operators in other 
> languages, which all do what you would expect).

Well, they are operators and there are only a limited number of special
characters available. Look at all the different uses of & and | in the
shell (8-(

Perhaps the syntax could have been " and " / " or " (as in Perl's
and / or statement qualifiers (something() or die "oops";), but it is
far too late to change sh syntax. We have to live with it or use a
different shell or language.

Far too often do I make the mistake of thinking that something can
easily be written using sh, getting deep into it and then wishing I'd
used perl instead. Yes, it is amazing what an expert can do with sh, but
developing and debugging a complex sh script can take far too long.

-- 
Adrian Wontroba
Nearly every complex solution to a programming problem that I
have looked at carefully has turned out to be wrong.
		-- Brent Welch
(First cookie out of the box, and almost appropriate.)



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20090718024835.GB8379>