3

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!

jyz
  • 6,011
  • 3
  • 29
  • 37
  • `sleep 20 && read ${variable}` ?? – Anders Sep 24 '10 at 11:10
  • 20 seconds is only an example. Let's pretend it needs to wait for 1 hour. But the guy is in front the PC to write the input, he doesn't need to wait the 1 hour.... got it? – jyz Sep 24 '10 at 11:13

2 Answers2

2

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"
alanc
  • 4,102
  • 21
  • 24
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • TMOUT is intended for interactive sessions, hence it says `after issuing the PS1 prompt`. It won't do anything in a script – Daenyth Sep 24 '10 at 15:13
  • @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 Sep 24 '10 at 15:29
1

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

fschmitt
  • 3,478
  • 2
  • 22
  • 24
  • Thanks mate it worked. I was wondering... since we're using ksh, is there a way to do this with coproccess? – jyz Sep 24 '10 at 11:37
  • The third post has `read -t` which the OP can't use. Unless you're not counting the question at that link, then the third *answer* isn't a solution. This kind of confusion is one of the reasons that it's preferred on SO to include the information from the link in your answer. – Dennis Williamson Nov 04 '10 at 03:12