Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

read timeout

瀏覽次數:298 次
跳到第一則未讀訊息

pingu

未讀,
2004年7月22日 上午8: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日 上午9: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日 凌晨4: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日 清晨5: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 則新訊息