2

I have a bash script, in which I have specified via trap that a function will be called for any (catchable) signal.

typeset -i sig=1
while (( sig < 65 )); do
    trap myfunc $sig
    let sig=sig+1
done

Is there any way my script can determine which signal has been caught?

2 Answers 2

8
trap "signum=${sig};myfunc" "$sig"
2
  • Brilliant. Thanks for thinking outside the box. Feb 6, 2015 at 14:16
  • I think it may be better this way trap "myfunc $sig" $sig or with a switch, instead of embedding name to get at $1. Feb 6, 2015 at 18:47
3

I'm assuming you're trying to pass the value of the signal you've caught to the action you want to take. Try something like this:

# 1     SIGHUP
# 2     SIGINT
# 3     SIGQUIT
# 6     SIGABRT
# 9     SIGKILL
# 14    SIGALRM
# 15    SIGTERM

for sig in 1 2 3 6 9 14 15; do
    trap 'echo "caught signal '$sig'..."' $sig
done
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .