shell 'trap' woodoo :)

Cristian Ionescu-Idbohrn cristian.ionescu-idbohrn at axis.com
Sat Sep 11 19:31:47 UTC 2010


This test script:

---8<---
#!/bin/sh

set -e
set -u
#set -x

trap 'wrapper_func' EXIT TERM INT

other_func() {
	echo "in : other_func" >&2
# XXX:	if this doesn't fail, the expected exit status comes out after 'trap'
#	but, if it _does_ fail, the exit status of the failed command will
#	be returned, as errexit will step in.
	ls -al /foo/bar/baz || :
	echo "out: other_func" >&2
}

wrapper_func() {
	echo "in : wrapper_func" >&2
	date +%s >/dev/null
	echo "1. : wrapper_func: listing traps" >&2
	trap
	echo "2. : wrapper_func: removing traps" >&2
	trap - EXIT INT TERM
	echo "3. : wrapper_func: listing traps again" >&2
	trap
	other_func
	echo "out: wrapper_func" >&2
}

echo "running: $0" >&2
sleep 5

exit 77
--->8---

does not behave as expected if the shell is ash, hush or dash.  Bash on
debian unstable:

	GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)
	Copyright (C) 2009 Free Software Foundation, Inc.
	License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

does TRT, though.

All shells run the trap action twice when hitting C-c with trap on INT.
With signals removed in the trap action, only bash behaves, according to
my experiments.


Cheers,

-- 
Cristian


More information about the busybox mailing list