2

I'd like to to do this in bash:

trap "echo Don\'t do that!" 2 3

which works just fine, except that I want the script to continue. How can I do that? If I leave the command as a blank string, the script continues, but does not print anything. Can I have both printing message and continuing?

2
  • If I understand this correctly, you are trying to create an uninterruptible script?
    – l0b0
    Jan 17, 2014 at 21:02
  • Yes. That tells the user not to even try to abort if s/he does. Jan 17, 2014 at 23:14

1 Answer 1

4

With this script:

#!/bin/bash
trap 'echo "Whee!"' 3 2

echo "Setting up.."
sleep 5
echo "Done."

I get this output:

Setting up..
^CWhee!
Done.

..when I sent a ^C during the sleep command. The interrupt is sent; bash traps it and continues, but the interrupt is properly handled by sleep. Is this not what you want?

2
  • No, that's not what I want. I want the script to continue processing as if the signal had been ignored, so in your example, the sleep should not be prematurely stopped but continue to sleep for the full amount. In my real code, obviously there isn't a 'sleep' but some algorithm that takes a while, and which should not be interrupted. Jan 17, 2014 at 19:48
  • 1
    I'm pretty sure in that case you need a new version of sleep that's resistant to SIGTERM and SIGINT. My understanding of this test case is that I send SIGINT, sleep catches but does not trap the interrupt, so sleep dies and the signal is inherited by its parent, bash, which catches and traps the signal, whereupon it proceeds to the next command.
    – DopeGhoti
    Jan 18, 2014 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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