2013-11-23 97 views
0

我正在研究一个bash脚本,并且由于某种原因,if语句总是成真。如果我运行下面的应用程序,并在提示我键入yes并回车,这里是打印出来:Bash脚本比较不起作用

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 

回答

3

问题出在-eq-eq用于数字比较。使用=代替, 所以

[[ $answer = "no" ]] 

代替

[[ $answer -eq "no" ]]