views:

66

answers:

2

Hi guys,

I need to wait for an input for 20 seconds, after that myscript should continue the execution.
I've tried using read -t20 var however this works only on bash. I'm using ksh on Solaris 10.

Can someone help me please?

EDIT: 20 seconds is only an example. Let's pretend it needs to wait for 1 hour. But the guy could or could not be in front the PC to write the input, he doesn't need to wait the 1 hour to enter an input, but if he's not in front of the PC so the shell should continue the execution after waiting for some time.

Thanks!

+1  A: 

Look at this forum thread it has the answer in the third post.

fschmitt
Thanks mate it worked. I was wondering... since we're using ksh, is there a way to do this with coproccess?
jyzuz
+2  A: 

From man ksh:

TMOUT
If set to a value greater than zero, the shell terminates if a command is not entered within the prescribed number of seconds after issuing the PS1 prompt. The shell can be compiled with a maximum bound for this value which cannot be exceeded.

I'm not sure whether this works with read in ksh on Solaris. It does work with ksh93, but that version also has read -t.

This script includes this approach:

# Start the (potentially blocking) read process in the background

    (read -p && print "$REPLY" > "$Tmp") &  readpid=$!

    # Now start a "watchdog" process that will kill the reader after
    # some time:

    (
        sleep 2; kill $readpid >/dev/null 2>&1 ||
        { sleep 1; kill -1 $readpid >/dev/null 2>&1; } ||
        { sleep 1; kill -9 $readpid; }
    ) &     watchdogpid=$!

    # Now wait for the reading process to terminate. It will terminate
    # reliably, either because the read terminated, or because the
    # "watchdog" process made it terminate.

    wait $readpid

    # Now stop the watchdog:

    kill -9 $watchdogpid >/dev/null 2>&1

    REPLY=TERMINATED            # Assume the worst
    [[ -s $Tmp ]] && read < "$Tmp"
Dennis Williamson
TMOUT is intended for interactive sessions, hence it says `after issuing the PS1 prompt`. It won't do anything in a script
Daenyth
@Daenyth: It works in a test script I tried (but if it times out it leaves the terminal in `-echo` state and I have to do a reset).
Dennis Williamson
@Dennis: Wow, that's terrible
Daenyth