D
D
di2021-06-15 17:40:46
bash
di, 2021-06-15 17:40:46

How not to stop script execution after trap?

this is the script. I want to be able to continue working after ctrl + c if the user did not confirm the exit

function after_ctrl_c() {
  echo
  # shellcheck disable=SC2162
  read -p "do you want to stop the program? [yes/no]: " answer
  case $answer in
  y | yes | Y | YES)
    echo Yes
    exit
    ;;
  *)
    echo No
    ;;
  esac
}

trap after_ctrl_c SIGINT

echo $$

while sleep 10
do
    echo Sleeping
done


I thought the while loop would continue if I didn't call exit. but it ends the job regardless of the choice

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Saboteur, 2021-06-15
@Delgus

while sleep 10
do
    echo Sleeping
done

You interrupt the sleep 10 command
after that your loop ends
Do this
while true; do
   sleep 10
   echo Sleeping
done

S
SOTVM, 2021-06-16
@sotvm

||it's not easier = one strike, but shorter))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question