Operating System - HP-UX
1833188 Members
2868 Online
110051 Solutions
New Discussion

How to make a script like "press any key within 10 seconds"?

 
SOLVED
Go to solution
Fragon
Trusted Contributor

How to make a script like "press any key within 10 seconds"?

I want to make a script, it will wait user's input, if exceed 10 seconds and user input nothing , the script will go-on with a default value!
How to do?
Thanks a lot and very sorry for my poor English!
3 REPLIES 3
John Poff
Honored Contributor
Solution

Re: How to make a script like "press any key within 10 seconds"?

Hi,

Take a look at the 'line' command. You can use the -t option to specify a timeout value in seconds. Here is a snippet from the man page:

The following lines in a shell script prompt for a file name and display information about the file:

echo 'Enter file name: \c'
reply=`line`
ls -l $reply

To limit the response time to 10 seconds, use the form:

reply=`line -t 10`

then test for no response. If no response occurs before timeout expires, a default behavior should be provided.



So all you would need to do is add an 'if' statement to test the variable and set a default value if it doesn't have a value.

JP
Mark Ellzey
Valued Contributor

Re: How to make a script like "press any key within 10 seconds"?

Hi,

Keep in mind that this 'line' command only works with sh and ksh. It will not work with csh.

Regards,
Mark
Ron Kuris
Occasional Advisor

Re: How to make a script like "press any key within 10 seconds"?

It works fine with csh also, as far as I can tell (of course, you have to use slightly different syntax):

#!/bin/csh
set line=`line -t 2`
echo $line