Bash Shell脚本-检测Enter键 [英] Bash Shell Scripting - detect the Enter key

查看:190
本文介绍了Bash Shell脚本-检测Enter键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将我的输入与 Enter / Return 键...

I need to compare my input with Enter/Return key...

read -n1 key
if [ $key == "\n" ]
   echo "@@@"
fi

但这无法正常工作.此代码有什么问题

But this is not working.. What is wrong with this code

推荐答案

发布的代码存在多个问题.内嵌评论详细说明了要解决的问题:

Several issues with the posted code. Inline comments detail what to fix:

#!/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

这篇关于Bash Shell脚本-检测Enter键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆