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?

Jolta
  • 2,620
  • 1
  • 29
  • 42
Johannes Ernst
  • 3,072
  • 3
  • 42
  • 56

1 Answers1

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?

DopeGhoti
  • 757
  • 7
  • 15
  • 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. – Johannes Ernst Jan 17 '14 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 '14 at 00:21