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

How do I write interactive korn shell script

Status
Not open for further replies.

Tison

Programmer
May 12, 1999
216
CH
I would like to write a shell script (ksh on AIX) which supplies a default parameter and then gives the user 10 seconds to respond with an alternative.<br>
Something like this ;<br>
answer=&quot;Y&quot;<br>
echo &quot;This app will close in 10 seconds&quot;<br>
echo &quot;Enter 'N' to abort&quot;<br>
wait 10 seconds<br>
if [ &quot;$answer&quot; = &quot;N&quot; ]<br>
exit<br>
else<br>
kill app<br>
fi<br>
<br>
<br>

 
I have been unsuccessful in doing this with ksh. However, it is possible to do, and I have done it, with Perl.
 
It <b>is</b> possible to catch signals in a Korn script.<br>
<br>
I've never done this (and only have a couple of minutes to answer the post) but how about something like this:<br>
<br>
Just before you're going to ask for input, start off a background process that knows yor process number and will wait ten seconds before sending you a signal.<br>
<br>
Set a flag that says &quot;I am waiting for input&quot;<br>
<br>
Wait for input from your user.<br>
<br>
As soon as you get input, unset that flag.<br>
<br>
If you get a signal whilst the flag is set - use the default value for the input and continue.<br>
<br>
-ml<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
I've used the signals technique before, and it works, mostly. I've had far better success asking the query in a language that understands timeouts (perl, expect, C), and returning a code the ksh script can interpret. I've seen a generic query/timeout program written in C that had a parameter list something like<br>
<br>
timeout &quot;Prompt for input&quot; timeout Validinput1 Validinput2...<br>
<br>
It would print to stdout the validinput entered if any was entered before timeout seconds had passed. With no validinput list it would print whatever was entered. If timeout happened first, it wouldn't print anything, ie, stdout got a null string.<br>
<br>
Unfortunately, I don't have a copy on hand. :(<br>
<br>
Write me if you need help developing perl source. I read, but don't write C.<br>
<br>
- Kai.<br>
mailto::<A HREF="mailto:kai_hintze@earthling.net">kai_hintze@earthling.net</A><br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top