[comp.unix.shell] Implementing a Timeout During Shell Script Data Entry

larry@kitty.UUCP (Larry Lippman) (10/21/90)

	I would be interested in seeing any suggestions for implementing
a timeout for lack of response to a "read" statement contained in a shell
script running under Bourne shell.  My purpose for such a requirement is
to permit default values to be automatically entered in a shell script
created for startup purposes without having any user entry whatsoever.
Such a script could, as an example, be run from /etc/rc, crontab, or
manually invoked.

	For example, the shell script would echo a data entry prompt and
wait a preset period of time for data entry (via stdin) to occur.  If no
data entry occurred, then a shell variable would be set to a default
value; if data entry did occur, the shell variable would be set to the
value returned by the read statement.

	Having tried unsuccessfully to accomplish the above by spawning
two processes (one for a sleep, and one for a read), and testing for child
process status (i.e., a return from sleep would kill the read process,
and a return from read would kill the sleep process, etc.), I wrote a
C program 5 years ago that I have been using ever since.  The C program
is called from a shell script with one argument for timeout in seconds,
with stdout consisting of a string obtained from characters grabbed from
stdin in raw mode; a null string is sent to stdout if no input occurs.

	However, I would really like some greater portability than the
above, and would like to create a situation where the entire function
can be contained with a Bourne shell script.

	Anyone have any suggestions?  A suitable solution needs to be
"bullet proof" and suitably trap and reset signals in the event of an
operator doing something dumb - a common occurence in the industrial
world! :-)

Larry Lippman @ Recognition Research Corp.  "Have you hugged your cat today?"
VOICE: 716/688-1231   {boulder, rutgers, watmath}!ub!kitty!larry
FAX:   716/741-9635                  {utzoo, uunet}!/      \aerion!larry

bernie@DIALix.oz.au (Bernd Felsche) (10/22/90)

The following seems to work for me:

#!/bin/sh  (or korny, if you have it)

  delay=6
  trap 'trap 14' 14
  echo "What is your Quest? \c"
  (sleep $delay; kill -14 $$ 2>/dev/null)&
  read YourQuest
  kill $! 2>/dev/null
  [ ! -z "$YourQuest" ] && echo "Bah. Humbug. Not interested in $YourQuest" \
  			|| echo "You must be interested in something!"

Hope that does the trick.

-- 
 ________Bernd_Felsche__________bernie@DIALix.oz.au_____________
[ Phone: +61 9 419 2297		19 Coleman Road			]
[ TZ:	 UTC-8			Calista, Western Australia 6167	]

morrell@hpcuhb.cup.hp.com (Michael Morrell) (10/23/90)

/comp.unix.shell / larry@kitty.UUCP (Larry Lippman) /  9:35 pm  Oct 20, 1990/

	I would be interested in seeing any suggestions for implementing
a timeout for lack of response to a "read" statement contained in a shell
script running under Bourne shell.
----------

You might try using the probably undocumented timeout feature of line(1).
I'm not sure which versions of Un*x have this "feature", but you could check.
It's used as follows:

  echo "Prompt\c"      [ or echo -n "Prompt" ]
  reply=`line -t 10`

The line command will return after 10 seconds if no input has been detected.

Hope this helps,

   Michael

Dan_Jacobson@ATT.COM (10/24/90)

>>>>> On 22 Oct 90 14:51:29 GMT, bernie@DIALix.oz.au (Bernd Felsche) said:

Bernd> The following seems to work for me:
[...]

Well, I just stuck this in the middle of my .profile this morning...
seems to work:

test -n "$program" &&
	sec=8 &&
	trap 'trap 2' 2 &&
	echo "you got $sec seconds to interrupt before
		\"$program\"... \c" &&
	sleep $sec &&
	trap 2 &&
	eval $program

Bernd> Hope that does the trick.
I hope so too... even though there might be tiny intervals where an
interrupt would spoil the fun.
-- 
Dan_Jacobson@ATT.COM  Naperville IL USA  +1 708-979-6364

davidsen@sixhub.UUCP (Wm E. Davidsen Jr) (10/24/90)

In article <592@DIALix.oz.au> bernie@DIALix.oz.au (Bernd Felsche) writes:

| #!/bin/sh  (or korny, if you have it)
| 
|   delay=6
|   trap 'trap 14' 14
|   echo "What is your Quest? \c"
|   (sleep $delay; kill -14 $$ 2>/dev/null)&
|   read YourQuest
|   kill $! 2>/dev/null
|   [ ! -z "$YourQuest" ] && echo "Bah. Humbug. Not interested in $YourQuest" \
|   			|| echo "You must be interested in something!"
| 
| Hope that does the trick.

  Works here under sh, not under ksh. No idea.
-- 
bill davidsen - davidsen@sixhub.uucp (uunet!crdgw1!sixhub!davidsen)
    sysop *IX BBS and Public Access UNIX
    moderator of comp.binaries.ibm.pc and 80386 mailing list
"Stupidity, like virtue, is its own reward" -me

emcguire@ccad.uiowa.edu (Ed McGuire) (10/24/90)

In article <38020001@hpcuhb.cup.hp.com> morrell@hpcuhb.cup.hp.com (Michael Morrell) writes:

> You might try using the probably undocumented timeout feature of line(1).
> I'm not sure which versions of Un*x have this "feature", but you could check.
> It's used as follows:
> 
>   echo "Prompt\c"      [ or echo -n "Prompt" ]
>   reply=`line -t 10`
> 
> The line command will return after 10 seconds if no input has been detected.

What version if UNIX are you running?  No machine at my site will do it.

	Alliant Concentrix 5.5 (BSD43)		no line(1)
	Apollo Domain/OS SR10.1/BSD43		no line(1)
	Digital ULTRIX 3.1			ignores -t
	Intergraph CLIX 3.0.8 (SYSVR31)		ignores -t
	S.G. IRIX (SYSVR3)			ignores -t
	Stardent OS 2.2 (SYSVR30)		ignores -t
	Sun SunOS (BSD43)			ignores -t
-- 
peace.  -- Ed
"Vote.  Because it's the Right Thing."

morrell@hpcuhb.cup.hp.com (Michael Morrell) (10/25/90)

/comp.unix.shell/emcguire@ccad.uiowa.edu (Ed McGuire) /  9:20 am  Oct 24, 1990
> You might try using the probably undocumented timeout feature of line(1).
> I'm not sure which versions of Un*x have this "feature", but you could check.

What version if UNIX are you running?  No machine at my site will do it.
----------

I'm using HP-UX 7.0 (a conglomeration of SysV and BSD4.something), but I fairly
sure it's not unique to HP-UX.  Also, "line" may be a shell builtin on some
systems, which would explain why you don't find a line(1).

   Michael

davidsen@sixhub.UUCP (Wm E. Davidsen Jr) (10/29/90)

The original suggestion works on Xenix in sh but not ksh.
-- 
bill davidsen - davidsen@sixhub.uucp (uunet!crdgw1!sixhub!davidsen)
    sysop *IX BBS and Public Access UNIX
    moderator of comp.binaries.ibm.pc and 80386 mailing list
"Stupidity, like virtue, is its own reward" -me