Command not found Error (stderr) while comparing string in Bash Script [duplicate]

自作多情 提交于 2020-01-04 16:58:23

问题


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

回答1:


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



回答2:


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




回答3:


You can use this in BASH:

[[ "$option" == [Yy] ]] && echo "YES" || echo "NO"


来源:https://stackoverflow.com/questions/27222542/command-not-found-error-stderr-while-comparing-string-in-bash-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!