linux - Shell 脚本 - 不要等待读取

标签 linux bash shell

这是一个应该倒计时 10 秒的循环 ($rtime)。 如果您想早点跳出循环,则必须键入 n。 我能以某种方式不等到读取得到输入吗,因为此刻循环总是 卡在读取并等待输入。

    ct=$(date +"%s")
    while [ $(( $ctn - $ct )) -le $rtime ] ; do
        ctn=$(date +"%s")
        read r 
        if [ "$r" = "n" ] ; then
            break
        fi
    done

最佳答案

您可以指定超时以读取:

read -t 1 r

如果一秒钟后没有输入任何内容,这将导致它完成。

关于linux - Shell 脚本 - 不要等待读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32355975/

相关文章:

linux - bash: && 运算符行为

c - Malloc 内存问题

linux - 提取没有同名目录的文件

linux - 无法在 shell 脚本中显示选项参数

Linux shell 计算 key 的简单方法

linux - 如何正确停止shell脚本?

linux - 在Linux中编辑vmlinuz文件

python - 每隔 x 小时运行一个进程

python - 使用 linux 服务和 python 文件每 12 小时重新启动 apache

找不到 Node.js