在Bash脚本中比较字符串时找不到命令错误(stderr) [英] Command not found Error (stderr) while comparing string in Bash Script

查看:34
本文介绍了在Bash脚本中比较字符串时找不到命令错误(stderr)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是从用户读取一个字符(可能是'Y','y','N','n').如果字符是 Y y ,则显示 YES .如果字符是 N n ,则显示 NO .

Problem is to read one character from the user (this may be 'Y', 'y', 'N', 'n'). If the character is Y or y display YES. If the character is N or n display NO.

原始问题

这是我的代码:

read option
if ["$option"=="y"] || ["$option"=="Y"]
then
    echo "YES"
else
    echo "NO"
fi

它引发错误 (stderr)

solution.sh: line 2: [Y=y]: command not found
solution.sh: line 2: [Y=Y]: command not found

推荐答案

read option
if [ "$option" == "y" ] || [ "$option" == "Y" ]
then
echo "YES"
elif [ "$option" == "n" ] || [ "$option" == "N" ]   
then
echo "NO"
else
echo "?"
fi

这篇关于在Bash脚本中比较字符串时找不到命令错误(stderr)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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