开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜