Reputation: 985
I want to provide user with a prompt to press any key or wait for a timeout to continue using shell. Usually this case is solved with following idiom:
read -rs -t3 -n1 "Press any key or wait to continue ..."
However this prompt seems a little bit clumsy to me and I would like to left only "Press any key to continue ..." part of a message and indicate timeout with dots printed each second. So I write following script:
#!/bin/sh
echo -n "Press any key to continue";
for _ in `seq 3`; do
if ! read -rs -n1 -t1 ; then echo -n "."; else break; fi
done
echo
It works just as I expect, but obviously there is too much code, so I have to put in separate file instead of using as sh -c "..."
in script. Is there a way to implement it in more concise and compact way?
P.S. Returning non-zero error code on Ctrl-C
pressed is a must.
Upvotes: 2
Views: 1395
Reputation: 15461
A bit more concise :
echo -n "Press any key to continue";
for _ in {1..3}; do read -rs -n1 -t1 || printf ".";done;echo
Upvotes: 2