zsh-workers
 help / color / mirror / code / Atom feed
From: "Nelson H. F. Beebe" <beebe@math.utah.edu>
To: zsh-workers@sunsite.dk
Cc: beebe@math.utah.edu
Subject: zsh-4.1.1 and trap '...' DEBUG: a bug or a feature?
Date: Tue, 23 Mar 2004 10:54:22 -0700 (MST)	[thread overview]
Message-ID: <CMM.0.92.0.1080064462.beebe@psi.math.utah.edu> (raw)

We are finishing up a book on shell programming, and in the course of
documenting the handling of DEBUG traps, I found unexpected behavior
in zsh-4.1.1, which I have installed on about 20 flavors of Unix at my
site.

Consider the following test file:

	% cat debug-trap
	trap 'echo This is an EXIT trap' EXIT
	trap 'echo This is a DEBUG trap' DEBUG
	pwd
	pwd
	pwd
	pwd

Now watch this behavior (recorded on an IA-64 system, unless otherwise
noted).

First, bash:

	% /local/build/icc/bash-2.05b.p7/bash --version
	GNU bash, version 2.05b.0(1)-release (ia64-unknown-linux-gnu)

	# That is the latest bash version 2.05b with all 7 released patches installed

	% /local/build/icc/bash-2.05b.p7/bash debug-trap
	This is a DEBUG trap
	/tmp
	This is a DEBUG trap
	/tmp
	This is a DEBUG trap
	/tmp
	This is a DEBUG trap
	/tmp
	This is a DEBUG trap
	This is an EXIT trap

Tests showed that the bash behavior depends critically on the patch
level; I'm in the process of upgrading my systems to have 2.05b with
all 7 patches.

Next zsh-4.1.1 (the latest):

	% zsh debug-trap
	This is a DEBUG trap
	/tmp
	This is a DEBUG trap
	/tmp
	This is a DEBUG trap
	/tmp
	This is a DEBUG trap
	/tmp
	This is a DEBUG trap
	This is an EXIT trap
	This is a DEBUG trap

Notice that zsh takes one final DEBUG trap that bash does not.  That
seems undesirable, because the EXIT trap code is documented in sh and
ksh to be the last code executed, and is invoked in response to either
normal termination, or an explicit exit statement.

Is this a bug, or a feature?

>From the zsh manual in the info system, I found this paragraph:

     If SIG is ZERR then ARG will be executed after each command with a
     nonzero exit status.  If SIG is DEBUG then ARG will be executed
     after each command.  If SIG is 0 or EXIT and the trap statement is
     executed inside the body of a function, then the command ARG is
     executed after the function completes.  If SIG is 0 or EXIT and
     the trap statement is not executed inside the body of a function,
     then the command ARG is executed when the shell terminates.

The last sentence implies that it is a feature, but perhaps it should
be reconsidered, and made to do what bash and ksh93 do, or else an
explanation should be offered in the manual of why a different choice
was made. Remember, unnecessary implementation differences create
portability headaches, and make life hard for documenters and authors
who have to explain, or even defend, the differences to their readers.

I checked that the extra invocation of the DEBUG trap does not spoil
the intended exit code:

	% cat exit.sh
	#! /bin/sh
	trap "ls -l $0; echo 'Check the exit status: it should be 49.'" EXIT
	trap "echo This is a DEBUG trap" DEBUG

	echo Leaving $0 with exit code 49
	exit 49

	% zsh ./exit.sh
	This is a DEBUG trap
	Leaving ./exit.sh with exit code 49
	This is a DEBUG trap
	-rwxrwxr-x  1 beebe staff 162 Mar 23 10:44 ./exit.sh
	This is a DEBUG trap
	Check the exit status: it should be 49.
	This is a DEBUG trap

	% echo $?
	49

POSIX (IEEE Std 1003.1-2001) does not include the DEBUG trap, so there
is no independent guidance there.

-------------------------------------------------------------------------------
- Nelson H. F. Beebe                    Tel: +1 801 581 5254                  -
- University of Utah                    FAX: +1 801 581 4148                  -
- Department of Mathematics, 110 LCB    Internet e-mail: beebe@math.utah.edu  -
- 155 S 1400 E RM 233                       beebe@acm.org  beebe@computer.org -
- Salt Lake City, UT 84112-0090, USA    URL: http://www.math.utah.edu/~beebe  -
-------------------------------------------------------------------------------


             reply	other threads:[~2004-03-23 17:54 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-23 17:54 Nelson H. F. Beebe [this message]
2004-03-23 18:20 ` Wayne Davison
2004-03-23 18:49 ` Peter Stephenson
2004-03-24 12:29   ` 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=CMM.0.92.0.1080064462.beebe@psi.math.utah.edu \
    --to=beebe@math.utah.edu \
    --cc=zsh-workers@sunsite.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).