I have a select menu with 4 options. I want that in case of pressing ctrl + c, remove some files before terminating the program. Here is my code:
#!/bin/bash
ctrl_c() {
test -f directory/file.txt && rm directory/file.txt
}
trap ctrl_c INT
PS3='Please enter your choice: '
while true; do
clear
options=("Option 1" "Option 2" "Option 3" "Exit")
select opt in "${options[@]}"
do
case $opt in
"Option 1")
echo "you chose choice $REPLY which is $opt"
break
;;
"Option 2")
echo "you chose choice $REPLY which is $opt"
break
;;
"Option 3")
echo "you chose choice $REPLY which is $opt"
break
;;
"Exit")
break 2
;;
*) echo "invalid option $REPLY";;
esac
done
read -p "Press [Enter] key to continue..."
done
but when I run this code and press ctrl + c, nothing happens and the program is not terminated, just ^c is typed. What's wrong?