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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timeout in a script

Status
Not open for further replies.

inforeqd

Technical User
Jan 8, 2001
95
US
I am looking for a function that i can put in to default an answer of yes in a
script. Currently I have a script that requires user interaction either with
a yes or no. I would like for a default value to be set so it times out if not
answered in 5 or so seconds to a yes answer
 
Hi inforeqd,

I'm not sure that this would solve your problem :

If the script is "myscript", you can make it automatic this way :

myscript << !
y
y
...
(the answer you want : yes or no)
!






 
Sorry for taking so long to reply to your question -- I've been thinking about it though.

I don't see a way to do this in the shell.

I *do* see how it could be done with an external program (or Perl script) that could be used to return a value into your shell script. Would you be interested in a solution like that? I could write you a Perl script to do that (I think) but you'd need someone else for a C program. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Thanks for the answers so far. I dont think i properly explained what i was looking for or I just dont understand the responses.

Basically,

I have a script.. the user is prompted with a line such as
do you want me to continu y or n?

If the user answer y then the script continues to the next step. If the user answers n then the script exits. I need to add a default setting that will allow the script to timeout and default to yes.

I have looked over expect but this needs to be somewhat universal. So im not going to be able to depend on that. Basically I need a timed prompt type function. Anything simple out there ? Ive looked over alot of things and nothing eludes to this.

TIA
Info
 
inforeqd,

Yep -- that's what I thought. I'm suggesting that the timed prompt function would have to be a separate program or script. You'd call it something like this.

print &quot;Are you sure?&quot;

# call the timed prompt program, user can enter Y or N
# and, if the user does not respond in 15 seconds, it will
# return a zero

timed_prompt YN 15
retval=$?

if [[ $retval == 0 ]]
then
# default taken after the timeout
elsif [[ $retval == 1 ]]
then
# user typed Y
elsif [[ $retval == 2 ]]
# user typed N
fi
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Mike,

Could i put the timed prompt infor you posted into a function?? and to the best of your knowledge would I have to add other things to the function before it would work?

Thanks alot anyhow. I guess this was one of those things that is a tough call for a simple shell script.

TIA
Infor
 
Infor,

Nope, timed_prompt would have to be a separate program or a Perl script. If you have Perl on your system (what is your UNIX system by the way?) or can install it I'd be happy to help you with writing it. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Mike,

I have perl and all .. I was just hoping to keep everything in one script
for ease of use. Im writing it to be multi-homed. So far what I have runs
on Solaris 2.5.1, Solaris 7 and Linux (redhat 6). But the option of a default
to yes would be nice. Oh well. I guess I could migrate the thing away from
bourne shell to perl... More to learn.

Thanks for all your help
Infor
 
well -- perl should still be portable for you, pity you need to keep it all in one script though. If you could have just the timed_prompt function in a separate file you wouldn't have to re-rewite the stuff you've already got. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top