zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.stephenson@samsung.com>
To: zsh-workers@zsh.org
Subject: Re: Function code breaking out of if then ...fi
Date: Mon, 05 Nov 2012 14:29:42 +0000	[thread overview]
Message-ID: <20121105142942.41d73d31@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <121102143911.ZM14168@torch.brasslantern.com>

n Fri, 02 Nov 2012 14:39:11 -0700
Bart Schaefer <schaefer@brasslantern.com> wrote:

> On Nov 2, 10:09am, Michal Maruska wrote:
> }
> } I wonder if the following behaviour is a bug, or
> } simply my wrong expectation:
> } 
> } This script, assuming the globbing fails, and I'm not using
> NULL_GLOB, } does not bother finishing the commands in the
> "then ....fi" block, } instead continues after "fi".
> 
> What slightly surprises me is that a glob failure isn't considered an
> error for purposes of ERR_EXIT (the -e option in your #! line).  I
> would have expected the whole script to quit at that point, but I
> guess glob errors are not treated as command failures because the
> command never executes in the first place.
> 
> However, what *is* happening is that a glob failure in a complex
> command acts in the manner of a "break" statement.

It's not really like a break, it's like (well, is) a global error flag
--- but the error status gets reset at the top level command loop, which
we reach in a script after the end of a set of complex commands --- but
only right at the end, when we're reading a new (possibly complex,
arbitrarily nested) command from the script.

I think actually what's going on could be considered a bug which is an
oversight for the case of scripts.  If the code was running a function,
the failure would cause it to skip right to the end of the function,
indeed any arbitrarily nested hierarchy of functions.  If we took the
rough equivalence between functions and scripts seriously, any hard
error (zerr() or zerrnam()) would cause a script to exit in the same
way.  Interactively, the error flag would still get reset but
non-interactively it wouldn't.

I don't see anything in the documentation to suggest that the top-level
command loop should be special non-interactively in the way it currently
is.  Indeed, when documenting try- and always-blocks, I wrote:

  An `error' in this context is a condition such as a syntax error
  which causes the shell to abort execution of the  current  func‐
  tion,  script,  or  list.
         ^^^^^^

which is clearly not true.  If there's a more fundamental description
of what happens on an error, I don't know where it is.

Indeed, it looks to me that without this the shell is violating the
"shall exit" terms of POSIX, section 2.8.1 of the shell command
language.  To use the example they give, this script

echo "${x!y}"
echo Got here

shouldn't print anything apart from the error message, but does.

pws


  parent reply	other threads:[~2012-11-05 14:40 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-02  9:09 Michal Maruska
2012-11-02 21:39 ` Bart Schaefer
2012-11-05  8:22   ` Han Pingtian
2012-11-05  9:31     ` Han Pingtian
2012-11-05 14:29   ` Peter Stephenson [this message]
2012-11-05 14:55     ` Peter Stephenson
2012-11-05 16:08       ` Peter Stephenson
2012-11-06  6:47         ` Han Pingtian
2012-11-09 22:52       ` Peter Stephenson
2012-11-10  1:13         ` Bart Schaefer
2012-11-10 21:23           ` Peter Stephenson
2012-11-12 10:24             ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20121105142942.41d73d31@pwslap01u.europe.root.pri \
    --to=p.stephenson@samsung.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).