Command not found Error (stderr) while comparing s

2019-07-20 23:42发布

This question already has an answer here:

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.

Original Problem

Here's my code:

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

It throws an Error (stderr)

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

3条回答
Luminary・发光体
2楼-- · 2019-07-21 00:04
read option
if [ "$option" == "y" ] || [ "$option" == "Y" ]
then
echo "YES"
elif [ "$option" == "n" ] || [ "$option" == "N" ]   
then
echo "NO"
else
echo "?"
fi
查看更多
再贱就再见
3楼-- · 2019-07-21 00:12

You can use this in BASH:

[[ "$option" == [Yy] ]] && echo "YES" || echo "NO"
查看更多
Deceive 欺骗
4楼-- · 2019-07-21 00:30

Leave space before ] and after [ in if statement.. if [ "$option"="y" ] || [ "$option"="Y" ]

查看更多
登录 后发表回答