dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jilles Tjoelker <jilles@stack.nl>
To: dash@vger.kernel.org
Subject: Re: exec command and error checking
Date: Tue, 28 Jan 2014 22:59:16 +0100	[thread overview]
Message-ID: <20140128215916.GA59221@stack.nl> (raw)
In-Reply-To: <20140128131759.GA17217@hal.lan>

On Tue, Jan 28, 2014 at 02:17:59PM +0100, Guido Berhoerster wrote:
> * Марк Коренберг <socketpair@gmail.com> [2014-01-28 13:16]:
> > $ dpkg -l | fgrep dash
> > ii  dash                                   0.5.7-2ubuntu2
> >                 POSIX-compliant shell

> > $ exec 9<no_such_file && echo TEST
> > dash: 1: cannot open no_such_file: No such file

> > $ exec 9<no_such_file || echo TEST
> > dash: 2: cannot open no_such_file: No such file

> > So, I cannot test this operation without using $?

> No, exec is a special built in and POSIX specifies that
> ...if a redirection error occurs (see Consequences of Shell
> Errors), the shell shall exit with a value in the range 1-125

> dash correctly exits with exit status of 2 as it should. ksh93,
> mksh, and pdksh do the same.

Indeed, this is correct.

You can avoid the exit by prepending 'command':
$ command exec 9<no_such_file || echo TEST
dash: 1: cannot open no_such_file: No such file
TEST

> > in BASH this works as expected (even in sh mode)

> That's either a bug or an intended deviation from the POSIX
> standard, you'll have to ask on the bug-bash list about that.

The inconsistency appears to be in the behaviour on fatal errors in
interactive shells. Strictly speaking, POSIX seems to require that the
shell continue execution with the next command, setting $? to a non-zero
value. Historically, behaviour has instead been to exit with a non-zero
status (if in a subshell) or return to the prompt with $? set to a
non-zero value (if in the top level shell). Dash implements the latter
and I think it is more useful.

Note that the two behaviours are indistinguishable if a single simple
command is entered.

-- 
Jilles Tjoelker

  parent reply	other threads:[~2014-01-28 22:05 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-28 12:16 exec command and error checking Марк Коренберг
2014-01-28 13:17 ` Guido Berhoerster
2014-01-28 15:32   ` Paul Gilmartin
2014-01-28 21:59   ` Jilles Tjoelker [this message]
2014-01-28 23:18     ` Guido Berhoerster
2014-01-29  3:19     ` Chet Ramey
2014-01-28 13:40 ` Seb
2014-01-28 14:05   ` Guido Berhoerster
2014-01-28 14:42     ` Seb
2014-01-28 14:52 ` Paul Gilmartin

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=20140128215916.GA59221@stack.nl \
    --to=jilles@stack.nl \
    --cc=dash@vger.kernel.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.
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).