Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

read timeout

296 views
Skip to first unread message

pingu

unread,
Jul 22, 2004, 8:41:20 AM7/22/04
to
Hi,

With bash you can do "read -t 5" to timeout read after 5 seconds.
However with bourne shell on AIX, you can't do that as there's no -t
option on the read.

I've been through some of the postings on here about this but none of
the suggestions have worked. The closest was:

stty raw
stty min 0 time 20 #'40' is for 4 seconds timeout
stty -echo
echo "Enter somthing : \c"
read var
stty -raw
stty echo
echo ${var}

The timeout works with this but you can't see what you're typing
because of the stty -echo. If you comment this line out, the timeout
doesn't work though.

Could someone suggest a bourne shell solution (not C or Korn) which
might be able to do this please?

Thanks a lot.

JS.

Stephane CHAZELAS

unread,
Jul 22, 2004, 9:32:48 AM7/22/04
to
2004-07-22, 05:41(-07), pingu:
[...]

> With bash you can do "read -t 5" to timeout read after 5 seconds.
> However with bourne shell on AIX, you can't do that as there's no -t
> option on the read.
[...]

> Could someone suggest a bourne shell solution (not C or Korn) which
> might be able to do this please?
[...]

Note that I don't think recent versions of AIX have a Bourne
shell (at least not in a usual place). Modern UNIXs now have a
POSIX conformant shell as /bin/sh (most often a ksh or ksh
derivative).

You could try something like:

read_timeout() {
trap : ALRM
trap 'kill "$pid" 2> /dev/null' EXIT
(sleep "$1" && kill -ALRM "$$") & pid=$!
read "$2"
ret=$?
kill "$pid" 2> /dev/null
trap - EXIT
return "$ret"
}
read_timeout 20 var
printf 'Got: "%s" as $var\n' "$var"

(untested)
There are race conditions, though.

--
Stephane

pingu

unread,
Jul 23, 2004, 4:42:55 AM7/23/04
to
Stephane CHAZELAS <this.a...@is.invalid> wrote in message news:<slrncfvgfv.1nc.s...@spam.is.invalid>...

Thanks for your reply. Unfortunately it didn't work. After the kill
-ALRM it still waits for input.

# ./test.sh
+ trap : ALRM
+ trap kill "$pid" 2> /dev/null EXIT
+ pid=901152
+ read var
+ sleep 2
+ kill -ALRM 647398

Stephane CHAZELAS

unread,
Jul 23, 2004, 5:56:06 AM7/23/04
to
2004-07-23, 01:42(-07), pingu:
[...]

>> read_timeout() {
>> trap : ALRM
>> trap 'kill "$pid" 2> /dev/null' EXIT
>> (sleep "$1" && kill -ALRM "$$") & pid=$!
>> read "$2"
>> ret=$?
>> kill "$pid" 2> /dev/null
>> trap - EXIT
>> return "$ret"
>> }
>> read_timeout 20 var
>> printf 'Got: "%s" as $var\n' "$var"
>>
>> (untested)
>> There are race conditions, though.
>
> Thanks for your reply. Unfortunately it didn't work. After the kill
> -ALRM it still waits for input.
[...]

Try after replacing ALRM with USR1.

--
Stephane

0 new messages