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

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

Post by Michael Wa » Wed, 16 Feb 2005 21:49:26



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

Quote:}

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

 
 
 

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

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.
--

2. Recommendations please for an ANSI/ISO compat. C++ compiler.

3. can anyone help: "BAD TRAP", trap type = 0x31

4. kprobes - kwatch points for 2.5.44

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

6. disk at once

7. trap signal but not change action?

8. eXodus X window viewer on Mac

9. >>>Compiling 2.0 problem: "asm86: Command not found" <<<

10. Trapping (catching) "kill" and "kill -HUP"

11. Trap "^C" "^Z" and "^\"

12. <font color="blue"><B>kmail</B></font> question

13. <font color="blue"><B>modemblaster</B></font>