2013-11-23 96 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" ]]