Shell 脚本 (bash/ksh):读取变量需要 20 秒

发布于 2024-09-24 10:54:03 字数 250 浏览 12 评论 0原文

我需要等待输入 20 秒,之后 myscript 应该继续执行。
我尝试过使用 read -t20 var 但这仅适用于 bash。我在 Solaris 10 上使用 ksh。

有人可以帮助我吗?

编辑:20秒只是一个例子。假设需要等待 1 小时。但是这个人可以或不能在PC前面写输入,他不需要等待1个小时来输入输入,但是如果他不在PC前面那么shell应该在等待后继续执行一段时间了。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑶笙 2024-10-01 10:54:03

来自 man ksh

TMOUT
如果设置为大于零的值,则如果在发出 PS1 提示符后规定的秒数内未输入命令,shell 将终止。可以使用该值的最大界限来编译 shell,该值不能超过。

我不确定这是否适用于 Solaris 上 ksh 中的 read。它确实适用于 ksh93,但该版本也有 read -t

此脚本包括以下方法:

# 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"

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"
成熟稳重的好男人 2024-10-01 10:54:03

查看此论坛帖子在第三篇文章中有答案。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文