bash shell的脚本 - 回车键/确认键 [英] Bash Shell Scripting - return key/Enter key

查看:2486
本文介绍了bash shell的脚本 - 回车键/确认键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要比较我与输入<大骨节病>输入 / <大骨节病>返回键...

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

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

但是,这是行不通的。什么是不对的code

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

推荐答案

在公布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的脚本 - 回车键/确认键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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