Created
January 1, 2020 16:18
-
-
Save RooSoft/5c2a79e9fd9c6d4a50ccc4cef1d09bdc to your computer and use it in GitHub Desktop.
How to trap ctrl-c in a bash script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
ctrlc_count=0 | |
function no_ctrlc() | |
{ | |
let ctrlc_count++ | |
echo | |
if [[ $ctrlc_count == 1 ]]; then | |
echo "Stop that." | |
elif [[ $ctrlc_count == 2 ]]; then | |
echo "Once more and I quit." | |
else | |
echo "That's it. I quit." | |
exit | |
fi | |
} | |
trap no_ctrlc SIGINT | |
while true | |
do | |
echo Sleeping | |
sleep 10 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment