How to re-prompt after a trap return in bash?

Posted by verbose on Stack Overflow See other posts from Stack Overflow or by verbose
Published on 2012-06-09T08:20:55Z Indexed on 2012/06/09 22:40 UTC
Read the original article Hit count: 237

Filed under:
|
|

I have a script that is supposed to trap SIGTERM and SIGTSTP. This is what I have in the main block:

trap 'killHandling' TERM

And in the function:

killHandling () {
    echo received kill signal, ignoring
    return
}

... and similar for SIGINT. The problem is one of user interface. The script prompts the user for some input, and if the SIGTERM or SIGINT occurs when the script is waiting for input, it's confusing. Here is the output in that case:

Enter something:     # SIGTERM received
received kill signal, ignoring

      # shell waits at blank line for user input, user gets confused
      # user hits "return", which then gets read as blank input from the user
      # bad things happen because of the blank input

I have definitely seen scripts which handle this more elegantly, like so:

Enter something:     # SIGTERM received
received kill signal, ignoring
Enter something:     # re-prompts user for user input, user is not confused

What is the mechanism used to accomplish the latter? Unfortunately I can't simply change my trap code to do the re-prompt as the script prompts the user for several things and what the prompt says is context-dependent. And there has to be a better way than writing context-dependent trap functions.

I'd be very grateful for any pointers. Thanks!

© Stack Overflow or respective owner

Related posts about bash

Related posts about shell