Giving "read" from standard input a timeout.

I want to prompt a user for input but I want it to timeout after a specified time if no response is given. I tried the sleep command but this does not work. I am using ksh.
Thanks.

#!/bin/sh
#
STTY=`stty -g`
WAIT=60
printf 'enter [y/n] '
stty intr '' -icanon min 0 time $WAIT ignbrk -brkint -ixon isig
read ans
stty $STTY
case ${ans:=y} in [yY]*) ;; *) exit ;; esac
echo do the rest
exit

or read timeout

You can use read timeout functionality to do this. We can make your requirement as,

#!/bin/sh
# input timeout

echo "Enter your input"
read -t 5 input
if [[ $? -ne 0 ]]
then
  echo "User did not enter any input"
  exit 1
else
  echo "User did input as $input"
fi

exit 0
# END #

Try this in bash.

HTH.

Thank you both for your help. I am still having trouble since neither works with ksh.
I have tried modifying both and still am not having much luck.

strange - works just fine for me under ksh on Solaris.
have you looked at the link I've posted?

here's another way - quoted from the posted link:

#!/bin/ksh

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

rello, what system are you using? If you have the line program, try:
ans=$(line)
rather than
read ans

I am running Solaris 9. All the programs are in ksh not /bin/sh.
I am also not trying to kill the process.
I have menu and one othe optoins calls a script that loops and pauses 5 seconds before repeating.
If we enter Ctrl d or break out we go back to the prompt and not the original menu.
I want to put a prompt at the end of the loop where it pauses (sleeps) for 5 seconds and prompt the user to quit (evaluate and quit) getting me back to the original menu.
I am not sure how ans=$(line) would work.
Thanks.

##Very shortened version of the program

!/bin/ksh
LIST="1;2;3"
REFRESH=5
clear
while [ "$quit" != "q" ]
do
echo "======================="

.......
for X in $LIST
do
......................
......................

fi
done
done
for I in $LIST
do
..................
..................
fi
done

   echo "======================="
   echo "hit delete to quit"
   sleep $REFRESH

done
"chanload.sh" 68 lines, 1405 characters

[quote=vgersh99]

#!/bin/sh
#
STTY=`stty -g`
WAIT=60
printf 'enter [y/n] '
stty intr '' -icanon min 0 time $WAIT ignbrk -brkint -ixon isig
read ans
stty $STTY
case ${ans:=y} in [yY]*) ;; *) exit ;; esac
echo do the rest
exit

[quote]

I am on an IBM p570 with AIX 5.3 and when for some reason I can only get a time delay of 20 seconds. If I use WAIT=200 I get twenty seconds as would be expected, however when I use WAIT=300 I get 4 seconds. Can anyone explain this to me?

Thanks

Just guess
WAIT=200 means 200/10=20 seconds
WAIT=300 means (300 mod 256)/10=4.5 secondes

That seems to be correct as 256 gives a zero wait time. How did you come up with that? or What is the logic behind the calculaton? :confused: