I've added the following to the top of my bash script:
quit() {
echo "Do you want to quit ? (y/n)"
read n
if [ "$n" = 'y' ]; then
exit
fi
}
trap quit INT
trap quit SIGINT
trap quit SIGTERM
The script asks the user a series of questions and then performs actions based on the results. Pressing CTRL+C seems to work some of the time.
But other times I just get Do you want to quit ? (y/n)
and the script locks up. This may happen when in an IF statement or within WHILE / DONE.
But it seems to be if you time the CTRL+C when an echo happens I can make the issue happen.. ie: CTRL+C at the same time as the echo.
Is there a way to always trap CTRL+C and prompt the user ? then let them decide if they want to quit or not ?
This code shows the issue happening..
quit() {
echo "Do you want to quit ? (y/n)"
read n
if [ "$n" = 'y' ]; then
exit
fi
}
trap quit INT
trap quit SIGINT
trap quit SIGTERM
for i in `seq 1 50`; do
sleep 1
echo -e "........"
read -i "0000" -e site
done
Try to time the CTRL+C just as the '........' and 0000 show on screen and the issue happens.
This seems to have made a big difference.
quit() {
while read -e -t 0.1; do : ; done
read -p "Do you want to quit ? (y/n) " n
if [ "$n" = 'y' ]; then
exit
fi
}
I'm struggling to make it happen now.
Thanks