Implementing a Timeout During Shell Script Data Entry

Larry Lippman larry at kitty.UUCP
Sun Oct 21 14:35:11 AEST 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.  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



More information about the Comp.unix.shell mailing list