开发者

Prompt the user for input and cancel prompt after waiting for x seconds

开发者 https://www.devze.com 2023-01-29 16:50 出处:网络
How can I prompt the user for input and continue with my script if no response is recieved in a preset amount of time.

How can I prompt the user for input and continue with my script if no response is recieved in a preset amount of time.

For example this is something like 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 an开发者_如何学编程swer y or n.";;
esac

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

I have vague thoughts that this may be possible by running the input in a subshell but I wouldn't know how to run a timeout at the same time. The timeout pseudo code could be something like:

sleep 200s
kill the prompt
continue... 


Use the -t option of read.

0

精彩评论

暂无评论...
验证码 换一张
取 消