How to timeout the "read" command

I have a script that at some point will ask the interactive user a question:
#!/bin/ksh
echo "What is your access code?"
read ans
...

Sometimes this script is run by other scripts and there are no interactive users. The script then hangs on the "read" command, waiting for a user response that will never come.

I would like to have the "read" timeout after a certain period of time, and move on to the next instruction.

Is there a way to do this?

Thanks all!

The box with similar threads at the bottom of this page has several promising-looking hits. In particular, Giving "read" from standard input a timeout. has a good discussion. However, you could also look at the test -t command to find out whether the script is running connected to a terminal or not.

If you are using ksh93 (and not pdksh or ksh88) you can use the read -t option to set a timeout.

Thanks all.

Era, I followed one of the links per your indications and found what I needed in a 3-2-2005 thread.

The "read -t" doesn't work for me (we don't have the correct ksh version).

However, this works:

ans=$(line -t 5)

I pauses 5 seconds before moving on if the user did not answer.

ans=$(line -t 5)
I have tried this option, but I am not timing out, can you please help.
I have KSH88
Thanks.

This works on ksh88 on my box:

line -t 5 | read ans

when line times out the $? (returned status code ) is one, otherwise the return code is 0.