سؤال

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!

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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"
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top