开发者

Shell Script (bash/ksh): 20 seconds to read a variable

开发者 https://www.devze.com 2023-01-17 16:04 出处:网络
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 o开发者_开发百科nly on bash. I\'m using ksh on Solaris

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 o开发者_开发百科nly 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!


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"


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号