I have a "main" bash script that runs another bash script (23.sh).
In the main script, I have a function that catches Ctrl+C operations and prompts for a password. If password is entered correctly, then it kills the main file.
However, I have an issue: when the user enters Ctrl+C twice in quick succession, the script freezes up (see comments in script below).
How do I deal with this? In essence, I would like the function ctrl_c() to be recursive so that when a user uses Ctrl+C when within the function, it still takes effect. I remember from a long time ago (some 10 years) that I was told that recursion in fuctions is a **bad idea!** Any solution?
#!/bin/bash
# This is the main file that runs data collection code 23.sh in an infinite loop
function ctrl_c() {
clear
echo "** Trapped CTRL-C0"
sleep 1s
echo "'''''''''''''''''''''''''''''''''''''''"
echo "Enter Admin password and click [ENTER]: "
# When user enters ctrl+c in here the program freezes up
read passPhrase
if [ $passPhrase == "pass" ]; then
echo "Password correct"
for i in
seq 1 5
; do
sleep 1
echo -n "."
# pkill 23.sh
# pkill main.sh
done
pkill main.sh
else
echo "Incorrect pass phrase. 'Service Selection' screen will in 5 seconds load"
for i in seq 1 5
; do
sleep 1
echo -n "."
done
# ./23.sh
fi
}
trap ctrl_c INT
while :
do
./23.sh
done
Asked by dearN
(131 rep)
Dec 11, 2017, 05:31 PM
Last activity: Jan 31, 2020, 06:23 PM
Last activity: Jan 31, 2020, 06:23 PM