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.
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
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
Try after replacing ALRM with USR1.
--
Stephane