3

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?

8
trap "signum=${sig};myfunc" "$sig"
  • Brilliant. Thanks for thinking outside the box. – BobDoolittle Feb 6 '15 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. – Nizam Mohamed Feb 6 '15 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.