autoconf-patches
[Top][All Lists]
Advanced

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

FYI: Some about trap


From: Akim Demaille
Subject: FYI: Some about trap
Date: 10 Nov 2000 16:53:24 +0100
User-agent: Gnus/5.0807 (Gnus v5.8.7) XEmacs/21.1 (Channel Islands)

Well, here is the snippet I'm adding.  For some reason I can't cvs
diff currently:

~/src/ace % cvsdiff |tee diffs                                   nostromo 16:52
SSH Version 1.2.27 [i586-unknown-linux], protocol version 1.5.
Standard version.  Does not use RSAREF.
nostromo: Reading configuration data /etc/ssh-nonfree/ssh_config
nostromo: ssh_connect: getuid 17023 geteuid 0 anon 0
nostromo: Connecting to subversions.gnu.org [198.186.203.17] port 22.
nostromo: Allocated local port 1023.
nostromo: connect: Connection refused
nostromo: Trying again...
nostromo: Connecting to subversions.gnu.org [198.186.203.17] port 22.
nostromo: Allocated local port 1023.
nostromo: connect: Connection refused
nostromo: Trying again...
nostromo: Connecting to subversions.gnu.org [198.186.203.17] port 22.
nostromo: Allocated local port 1023.
nostromo: connect: Connection refused
cvs [diff aborted]: received interrupt signal

2000-11-10  Akim Demaille  <address@hidden>

        * doc/autoconf.texi (Limitations of Builtins): Some information
        about `trap'.
        Document the Free BSD bug observed by Pavel.



@item @command{trap}
@cindex @command{trap}
It is safe to trap at least the signals 1, 2, 13 and 15.  You can also
trap 0, i.e., have the trap run when the script end (either via an
explicit @command{exit}, or the end of the script).

Although @sc{posix} is not absolutely clear on that point, it is widely
admitted that when entering the trap @samp{$?} should be set to the exit
status of the last command run before the trap.  The ambiguity can be
summarized as: ``when the trap is launched by an @command{exit}, what is
the @emph{last} command run: that before @command{exit}, or exit itself.

Bash considers @command{exit} was the last command, while Zsh and
Solaris 8 @command{sh} consider that when the trap is run it is
@emph{still} in the @command{exit}, hence it is the previous exit status
that the trap receives:

@example
% cat trap.sh
trap 'echo $?' 0
(exit 42); exit 0
% zsh trap.sh
42
% bash trap.sh
0
@end example

The portable solution is then simple: when you want to @samp{exit 42},
run @samp{(exit 42); exit 42}, the first @command{exit} being used to
set the exit status to 42 for Zsh, and the second to trigger the trap
and pass 42 as exit status for Bash.

Note that in Bourne shell an unqualified @command{exit} is equivalent to
@samp{exit $?}, hence you may actually abbreviate it as @samp{(exit 42);
exit}.

The shell in FreeBSD 4.0 has the following bug: @samp{ $?} is reset to 0
by empty lines if the code in inside trap.

@example
$ trap 'false

echo $?' 0
$ exit
0
@end example

@noindent
Fortunately this bug affects only trap.



reply via email to

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