Ask user to enter and cancel invitation after waiting x seconds

How can I ask a user for input and continue working with my script if no response is received within a specified period of time.

For example, this is what I have at the moment:

read -p "Would you like to reboot? (y/n) " yn
case $yn in
    [Yy]* ) echo "shutting down"; break;;
    [Nn]* ) echo "cancelled shutdown"; break;;
    * ) echo "Please answer y or n.";;
esac

However, I want the invitation to sit on the terminal for 2 minutes, and then if the response is not received, continue with my script.

I have vague thoughts about how this is possible by starting the subshell entry, but I don’t know how to start the timeout at the same time. The timeout pseudocode might look something like this:

sleep 200s
kill the prompt
continue... 
+3
source share
1 answer

Use the option -tfor read.

+2

Source: https://habr.com/ru/post/1779589/


All Articles