zsh-users
 help / color / mirror / code / Atom feed
From: "Bart Schaefer" <schaefer@candle.brasslantern.com>
To: "Andrej Borsenkow" <Andrej.Borsenkow@mow.siemens.ru>,
	"Akim Demaille" <akim@epita.fr>, <zsh-users@sunsite.auc.dk>,
	Peter Stephenson <pws@csr.com>
Subject: Re: Trap and exit
Date: Fri, 27 Oct 2000 15:26:04 +0000	[thread overview]
Message-ID: <1001027152604.ZM15350@candle.brasslantern.com> (raw)
In-Reply-To: <000401c0401b$613546b0$21c9ca95@mow.siemens.ru>

On Oct 27,  3:37pm, Andrej Borsenkow wrote:
}
} > /tmp % cat foo.sh
} > nostromo 12:42
} > #! /bin/sh
} > trap 'echo trap: $?' 0
} > exit 59
} > /tmp % zsh ./foo.sh
} > nostromo Err 59
} > trap: 0
} 
} It looks like a bug in Zsh. Quoting Unix 98:
} 
} =============
} The environment in which the shell executes a trap on EXIT will be identical
} to the environment immediately after the last command executed before the trap
} on EXIT was taken.
} =============
} 
} In this case last command was 'exit 59'.

Here's where we get into a matter of semantics; zsh doesn't consider the
"exit" command to have finished executing until the shell has actually
exited, so "last command BEFORE the trap" was in this case the trap
command itself.  The trap on exit is taken *during* the exit command,
not *after* it.

} What is exit code of _this_ command?

What does "_this_" mean?  Peter seems to have taken it to mean "the entire
script" whereas you seem to have meant "the `exit' command itself."

} Quoting Unix 98 again:
} 
} =============
} The exit status will be n, if specified. Otherwise, the value will be the exit
} value of the last command executed, or zero if no command was executed. When
} exit is executed in a trap action, the last command is considered to be the
} command that executed immediately preceding the trap action.
} =============

This appears to mean that:

	#!/bin/zsh
	trap 'true; exit' 0
	false

*should* always give $? = 1, because the last command executed before *the
trap* will have been "false", whereas zsh in this case *does* give $? = 0,
because the last command before the explicit "exit" is "true".

That one could be quite annoying to fix.

While we're on the topic:

zsh% TRAPEXIT() { echo trap called }
zsh% (true)
zsh% (true;exit)
trap called
zsh%

Is the exit trap supposed to be triggered on a subshell exit?  Is an
explicit call to "exit" supposed to make this difference?

Note also that this only happens with TRAPEXIT, and not with `trap ... 0'.
Is *that* intentional?

On Oct 27,  2:19pm, Peter Stephenson wrote:
}
} From this it looks like the status of the trap is the exit value of the
} exit command --- not of the function.  I'm not sure that's even defined.

I'm not even sure how to parse your sentence.

On Oct 27,  3:28pm, Akim Demaille wrote:
}
} Whatever you decide, please remain compatible with the workaround used
} in Autoconf: using `(exit 77); exit 77' instead of `exit 77' must have
} the desired property: the trap is triggered, and it receives $? = 77.

That's true now, and shouldn't become a problem.

On Oct 27,  5:39pm, Andrej Borsenkow wrote:
}
} > But then, you could argue that the false was the last command before the
} > script exited, and is hence the one referred to in this case --- given that
} > this is exactly what happens with an implicit exit by falling off the end.
} 
} The last command executed was `exit' not `false'.

No, that's my point made above.  The exit command has not finished executing
until the shell actually exits, at which point it's too late to consider it
to have executed before the trap.

} > To summarise: bleah.
} 
} Well, it adds to functionality in any case;

I don't follow this, I'm afraid.  Which is "it", and what does it add?

} and your example IMHO counts as bug.

I still think there's room for disagreement, unless you can show me where
the spec says that the exit command is considered to have finished before
the exit trap is called, even though the shell hasn't exited yet.

My example, however, does look like an actual bug, or at least an actual
nonconformance to that spec, which is not exactly the same thing.

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

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


      reply	other threads:[~2000-10-27 15:36 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-10-27 10:44 Akim Demaille
2000-10-27 11:37 ` Andrej Borsenkow
2000-10-27 11:49   ` Andrej Borsenkow
2000-10-27 12:47     ` Akim Demaille
2000-10-27 13:05       ` Andrej Borsenkow
2000-10-27 12:46   ` Akim Demaille
2000-10-27 13:06     ` Andrej Borsenkow
2000-10-27 13:19   ` Peter Stephenson
2000-10-27 13:28     ` Akim Demaille
2000-10-27 13:39     ` Andrej Borsenkow
2000-10-27 15:26       ` Bart Schaefer [this message]

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=1001027152604.ZM15350@candle.brasslantern.com \
    --to=schaefer@candle.brasslantern.com \
    --cc=Andrej.Borsenkow@mow.siemens.ru \
    --cc=akim@epita.fr \
    --cc=pws@csr.com \
    --cc=zsh-users@sunsite.auc.dk \
    /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).