问题是从用户读取一个字符(可能是'Y', 'y', 'N', 'n'
)。如果字符为Y
或y
则显示YES
。如果字符为N
或n
则显示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
答案 0 :(得分:3)
read option
if [ "$option" == "y" ] || [ "$option" == "Y" ]
then
echo "YES"
elif [ "$option" == "n" ] || [ "$option" == "N" ]
then
echo "NO"
else
echo "?"
fi
答案 1 :(得分:2)
在if ]
之前和[
之后留出空格。if [ "$option"="y" ] || [ "$option"="Y" ]
答案 2 :(得分:2)
你可以在BASH中使用它:
[[ "$option" == [Yy] ]] && echo "YES" || echo "NO"