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

Bash Shell脚本 – 返回键/ Enter键

发布时间:2020-12-15 18:46:52 所属栏目:安全 来源:网络整理
导读:我需要将输入与输入/返回键进行比较 read -n1 keyif [ $key == "n" ] echo "@@@"fi 但这不工作..这个代码有什么问题 发布代码的几个问题.内联评论详细说明要修复的内容: #!/bin/bash # ^^ Bash,not sh,must be used for read optionsread -s -n 1 key # -s
我需要将输入与输入/返回键进行比较
read -n1 key
if [ $key == "n" ]
   echo "@@@"
fi

但这不工作..这个代码有什么问题

发布代码的几个问题.内联评论详细说明要修复的内容:
#!/bin/bash 
# ^^ Bash,not sh,must be used for read options

read -s -n 1 key  # -s: do not echo input character. -n 1: read only 1 character (separate with space)

# double brackets to test,single equals sign,empty string for just 'enter' in this case...
# if [[ ... ]] is followed by semicolon and 'then' keyword
if [[ $key = "" ]]; then 
    echo 'You pressed enter!'
else
    echo "You pressed '$key'"
fi

(编辑:李大同)

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

    推荐文章
      热点阅读