加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 按任意键在5秒内中止

发布时间:2020-12-16 01:19:01 所属栏目:安全 来源:网络整理
导读:嗨,我正在尝试实施一个在5秒倒计时后发生的事件,除非按下一个键.我一直在使用这段代码,但是如果我按下回车键或空格键就会失败.在输入或空间被检测为“”的意义上它失败了. echo "Phoning home..."key=""read -r -s -n 1 -t 5 -p "Press any key to abort in
嗨,我正在尝试实施一个在5秒倒计时后发生的事件,除非按下一个键.我一直在使用这段代码,但是如果我按下回车键或空格键就会失败.在输入或空间被检测为“”的意义上它失败了.
echo "Phoning home..."
key=""
read -r -s -n 1 -t 5 -p "Press any key to abort in the next 5 seconds." key
echo
if [ "$key" = "" ]     # No Keypress detected,phone home.
     then python /home/myuser/bin/phonehome.py
     else echo "Aborting."
fi

看完这篇文章后,
Bash: Check if enter was pressed

我放弃了,发布在这里.我觉得必须有比我试图实施的更好的方法.

阅读手册说:

The return code for read is zero,unless end-of-file is encountered
or read times out.

在您的情况下,当用户在允许的时间内点击任何键时,您希望中??止,否则继续.

#!/bin/bash
if read -r -s -n 1 -t 5 -p "TEST:" key #key in a sense has no use at all
then
    echo "aborted"
else
    echo "continued"
fi

参考:
Read Manual

注意:引文的重点是我的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读