[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] too paranoid?
From: |
Stephane Chazelas |
Subject: |
Re: [Help-bash] too paranoid? |
Date: |
Fri, 11 Mar 2016 12:04:16 +0000 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
2016-03-10 17:48:37 -0700, Bob Proulx:
[...]
> unset tmpfile
> cleanup() {
> test -n "$tmpfile" && rm -f "$tmpfile"
> }
> trap "cleanup" EXIT
> trap "cleanup; trap - HUP; kill -HUP $$" HUP
> trap "cleanup; trap - INT; kill -INT $$" INT
[...]
Note that if sh is based on bash (GNU systems, OS/X...), pdksh
(some BSDs) or ksh93 (Solaris 11), (not dash nor zsh nor yash)
cleanup will be run twice upon a signal (once for the signal
handler, another time for the EXIT trap).
Depending on the version of pdksh/mksh, the "kill" within the
trap may not work at all, or if it works, the exit status of the
script may be that of the clean-up function.
For portability, it may be better as (untested):
unset tmpfile killedby
cleanup() {
ret=$?
if [ -n "$tmpfile" ]; then
rm -f "$tmpfile"
unset tmpfile
fi
if [ -n "$killedby" ]; then
sig=$killedby
unset killedby
trap - "$sig"
kill -s "$sig" "$$"
ret=$((128 + $(kill -l "$sig")))
fi
exit "$ret"
}
trap cleanup EXIT
for sig in INT TERM QUIT HUP; do
trap "killedby=$sig; cleanup" "$sig"
done
Note that doing a "kill $$" instead of exit(128+signum) is only
marginally useful. It is possibly useful for SIGINT and SIGQUIT
if your system has shells that implement the "wait and
cooperative exit" way of handling death by keyboard interrupt.
But note that shells like mksh will anyway do a exit(128+signum)
when they receive a signal.
--
Stephane
- [Help-bash] too paranoid?, John McKown, 2016/03/10
- Re: [Help-bash] too paranoid?, Stephane Chazelas, 2016/03/10
- Re: [Help-bash] too paranoid?, Bob Proulx, 2016/03/10
- Re: [Help-bash] too paranoid?,
Stephane Chazelas <=
- Re: [Help-bash] too paranoid?, Bob Proulx, 2016/03/12
- Re: [Help-bash] too paranoid?, Stephane Chazelas, 2016/03/14
- Re: [Help-bash] too paranoid?, Bob Proulx, 2016/03/19
- Re: [Help-bash] too paranoid?, Stephane Chazelas, 2016/03/21
- Re: [Help-bash] too paranoid?, Bob Proulx, 2016/03/21
- Re: [Help-bash] too paranoid?, Stephane Chazelas, 2016/03/21