我正在处理一个bash脚本,由于某种原因if语句总是成真。如果我运行以下应用程序并在提示符下输入yes并按Enter键,这里打印出来的内容:
The code will be checked out in the location the file is in, is this ok? (yes or no)
yes
yes
Stopping app
这是我的代码的样子。我在这里错过了什么吗?
echo "The code will be checked out in the location the file is in, is this ok? (yes or no)"
read answer
echo $answer
if [[ $answer -eq "no" ]] ; then
echo "Stopping app"
exit 1
fi
答案 0 :(得分:3)
问题在于-eq
。 -eq
用于数字比较。请改用=
,
所以
[[ $answer = "no" ]]
而不是
[[ $answer -eq "no" ]]