gpt4 book ai didi

Bash Shell 脚本 - 检测 Enter 键

转载 作者:行者123 更新时间:2023-11-29 08:45:35 28 4
gpt4 key购买 nike

我需要将我的输入与 Enter/Return 键进行比较...

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

关于Bash Shell 脚本 - 检测 Enter 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2612274/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com