【发布时间】:2014-07-15 14:20:27
【问题描述】:
我在处理一个简单的y/n 问题时遇到了问题。考虑以下代码:
echo "Hi there"
read ans
if [[ $ans != "y" || $ans != "Y" || $ans != "YES" || $ans != "yes" ]]; then
echo "Foo"
exit 0
fi
我已经看过——我会争辩——一些关于 StackOverflow 的信息更丰富的答案以寻求建议:Simple logical operators in Bash
我尝试了所有不同类型的变体,例如:
if [[ ($ans != "y" || $ans != "Y" || $ans != "YES" || $ans != "yes") ]]; then
echo "Foo"
exit 0
fi
if [[ ($ans != "y*" || $ans != "Y*" || $ans != "YES*" || $ans != "yes*") ]]; then
echo "Foo"
exit 0
fi
if [[ ($ans != "y") || ($ans != "Y") || ($ans != "YES") || ($ans != "yes") ]]; then
echo "Foo"
exit 0
fi
无论我为什么在这些情况下输入,它都会自动失败,我不知道为什么。如果有人有更好的方法来处理 y/n 答案,请告诉我!理想情况下,我想使用模式匹配(就像我可能使用 Perl 一样),但我不完全确定完成简单 y/n 问题的最佳方式/最有效方式。
【问题讨论】:
标签: bash if-statement conditional