From Nick Moffitt on Mon, 29 Mar 1999
How do I get a shell script to ignore ^C and ^Z?
"The software is intended to be as unobtrusive, unintrusive and
unconstraining as possible. In software as elsewhere, good
engineering is whatever gets the job done without calling attention to
itself."
-- Cynbe ru Taren, on Citadel (https://zork.net/cit/citadel.txt)
Well, with limitations you can do it with the following:
trap "" 2 20
As in this simple script example:
#!/bin/bash trap "" 2 20 while : ; do echo -n . sleep 1 done
All I'm doing is setting the INTerrupt and terminal stop signal handlers to a null string (to ignore [Ctrl]+[C]) and [Ctrl]+[Z]).
The rest of the script just prints an endless stream of dots at one second intervals to give you a chance to play with the keyboard.
You see, your default terminal settings "cook" the [Ctrl]-[C] into a SIGINT (generate a interrupt signal to the foreground task) and [Ctrl]-[Z] to a SIGTSTP. The default signal handlers for these "cancel/exit" and "suspend" respectively.
This technique will also prevent 'kill -INT' and 'kill -TSTP' from having their normal affect on these processes.
You could also do something like this using
stty susp 0 intr 0
... which merely changes the terminal settings so that these keystrokes are no longer "cooked" into their usual signals.
I believe that these tricks are inherently subject to race conditions (there is a finite and "exploitable" amount of time between the start of the script's execution and the time that these commands have their effect.
So I think that they should not be used in any attempt to provide security through some notion of an "unbreakable" shell script.
1 | 2 | 3 | 4 | 5 | 6 |