Google 网上论坛不再支持新的 Usenet 帖子或订阅项。历史内容仍可供查看。

read timeout

已查看 298 次
跳至第一个未读帖子

pingu

未读,
2004年7月22日 08:41:202004/7/22
收件人
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

未读,
2004年7月22日 09:32:482004/7/22
收件人
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

未读,
2004年7月23日 04:42:552004/7/23
收件人
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

未读,
2004年7月23日 05:56:062004/7/23
收件人
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 个新帖子