I need to know is it possible to interrupt a bash script using keys like ESC
or ENTER
? By sending SIGINT /CTRL + C
I am able to do, but due to some reasons(Check note in the last line) I cannot use CTRL +C
. So I need to have some custom way to cause an interrupt.
In other terms: In following script, cleanup
function is called when CTRL + C
is pressed. Now need to modify this behavior so that cleanup
function should be called when some keys like ENTER
OR ESC
is pressed.
cleanup() {
#do cleanup and exit
echo "Cleaning up..."
exit;
}
echo "Please enter your input:"
read input
while true
do
echo "This is some other info MERGED with user input in loop + $input"
sleep 2;
echo "[Press CTRL C to exit...]"
trap 'cleanup' SIGINT
done
Query:
- Is it possible to use custom keys for causing interrupts in bash?
- If possible, how to achieve it?
Note:
Reason: This script is called from another C++ program which has its own trap handling. So the trap handling of this script is conflicting with the parent program and ultimately the terminal is getting hung. In my organization that program's code is frozen so I cannot change its behavior. I have to tweak this child script only.