Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Answer Prompt Timeout 1

Status
Not open for further replies.

inforeqd

Technical User
Jan 8, 2001
95
US
I am rewriting a shell script into perl and I need to
know how to place a timeout function into the script.
Basically I have this

print "Now we will begin the tests, do you wish to continue (y/n)";

chop ($answer = <STDIN>);

if ($answer = &quot;yes&quot;) {
print &quot;We will continue&quot;;
}

DO SOME STUFF HERE

if ($answer = &quot;no&quot;) {
die &quot;bye bye&quot;
}

Basically I need to put a timeout that defaults to yes in order to DO SOME STUFF HERE. Can this be done in perl without creating a seperate file???

I was thinking of a new format using the if/else/elsif but I really am not sure of the best way.

TIA
Info
 
To solve this problem you need to read from STDIN in non-blocking mode. This can be done on a Unix system with 'fcntl'. The following waits 5 seconds for user input.

use Fcntl;
fcntl(STDIN, F_SETFL, O_NONBLOCK);
print &quot;Now we will begin the tests, do you wish to continue (y/n)&quot;;

for ($i=0; $i<5; $i++) {
$answer = <STDIN>;
last if (defined($answer));
sleep(1);
}

chomp($answer);
if ($answer eq &quot;n&quot;) {
die &quot;bye bye&quot;
} else {
print &quot;We will continue \n&quot;;
}
 
Raider,

Thanks, that worked great and really reduced the size of the shell script.

 
One more question that I forgot to include in my reply back. How would i put a default loop in there. In the shell script its a while loop that basically takes in y or n and if answered n it skips to the next test. If you have a quick and dirty for this in perl much appreciatos.
 
I'm not 100% sure what you want, but I think you want to enclose the whole process in a loop. This can be done in several ways. Here's one way:

use Fcntl;
fcntl(STDIN, F_SETFL, O_NONBLOCK);

@TestList = (&quot;test1&quot;, &quot;test2&quot;, &quot;test3&quot;);
foreach $Test (@TestList) {
for ($i=0; $i<5; $i++) {
$answer = <STDIN>;
last if (defined($answer));
sleep(1);
}
chomp($answer);
if ($answer eq &quot;n&quot;) {
print &quot;Skipping $Test \n&quot;;
next; # go to next test
} else {
print &quot;Running $Test \n&quot;;
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top