trap "action; exit" signals = trap "action" exit?

trap "action; exit" signals = trap "action" exit?

Post by Michael Wa » Fri, 17 Oct 2003 01:27:25



I would like to know your opinion whether

(1) trap "action; exit" signals

is equivalent with

(2) trap "action" exit

With exit trap, upon receiving a signal, the default behavior
is exit, then action is executed. So I prefer to use (2), but
did I miss anything? Is there a reason to use (1)? Thanks.
--

 
 
 

trap "action; exit" signals = trap "action" exit?

Post by Dan Merce » Fri, 17 Oct 2003 02:36:05


: I would like to know your opinion whether
:
: (1) trap "action; exit" signals
:
: is equivalent with
:
: (2) trap "action" exit
:
: With exit trap, upon receiving a signal, the default behavior
: is exit, then action is executed. So I prefer to use (2), but
: did I miss anything? Is there a reason to use (1)? Thanks.
: --

With the former,  the shell exit code will be the exit code of action.
With the latter,  the exit code will be whatever it was when the trap
blew.

Dan Mercer

 
 
 

1. trap "<new trap action>; $(get_old_trap SIGNAL)" SIGNAL

I want to be able to add new trap to existing trap by saying:

trap "<new trap action>; $(get_old_trap SIGNAL)" SIGNAL

Please comment if the following function get_old_trap()
covers all cases. This only works with ksh93, and that is ok
for me. This is because:

(1) bash as of the current release (v3.0) is not POSIX compliant as
demonstrated by:

--
trap 'echo a' exit
a=$(trap)
echo $a
/* no output */
--

POSIX requires that the old trap is saved in $a.

(2) while ksh88 saves the trap, it does not in a function:

--
$ /bin/ksh # Version M-11/16/88i
$ trap 'echo a' exit
$ a=$(trap)
$ echo $a
trap -- 'echo a' EXIT
$ a() { trap; }
$ a
--

Thanks.

get_old_trap() {
  typeset line signal="$1" old_trap=/tmp/$$.$RANDOM
  trap > $old_trap
  exec 3< $old_trap
  rm $old_trap
  while read -u3 -r -- line; do
    [[ "$line" == *?(SIG)$signal ]] || continue
    line=${line#trap --+( )}
    eval line=${line%+( )?(SIG)$signal}
    printf "%s\n" "$line"
    return 0
  done
  return 1

trap "echo trap1" EXIT
trap "echo trap2; $(get_old_trap EXIT)" EXIT
--
For low fair air travel, take Independence Air - http://www.flyi.com/.

2. getprocs() and compare_and_swap() functions

3. What is this supposed to return: "trap 'exit ' 0; exit"?

4. Anyone Using Back Up Tape Drive Compatible with Macs

5. GETSERVBYNAME()????????????????????"""""""""""""

6. My First Script

7. syslogd.conf does not support "send snmp trap" as an action

8. How to gracefully exit X/Linux

9. """"""""My SoundBlast 16 pnp isn't up yet""""""""""""

10. "ls" output or "cut" action: restricting to one line?

11. Missing in action: "ar" and "ranlib"...

12. trap "exit" 0 1 2 3 5 safe enough?

13. "source" exits script, need to trap