2017-05-03 39 views
0

我想比較一個立即在界限,我不明白爲什麼它不工作。已嘗試將變量放在「」仍然相同的錯誤中,並將(())與<>用作操作數。負數爲-9和我收到的錯誤是:比較bash腳本中的負數。不確定什麼錯誤的意思

./valsplit.sh: line 89: [[: −9: syntax error: invalid arithmetic operator (error token is "??9") 

這是我的腳本:

checkImmediate(){ 
lowerBound=-32768 
upperBound=32767 
if [[ $immediate -lt $lowerBound || $immediate -gt $upperBound ]]; then 
    hasErrors=1 
    errors+=("Out of bounds immediate. Immediates range between −32768 and 32767.") 
fi 
} 

我使用OSX。

任何幫助將不勝感激。 謝謝

+0

奇怪的是,在我的機器上,我沒有得到相同的代碼錯誤。我立即設置= -9,我也在使用OS X – PMonti

+0

嘗試將變量放在引號中。所以「$立即」和「$ lowerBound」等 –

+0

印度-9,上面提到:) PMonti感謝您抽出時間 –

回答

1

你的語法是好的,這是你的價值觀是錯誤的。這裏有一個例子:

$ var='1 –9' # this is a Unicode dash 
$ [[ $var -lt 5 ]] 
-bash: [[: 1 –9: syntax error: invalid arithmetic operator (error token is "–9") 

值從您發佈的錯誤都有一個統一劃線,如果你看一下set -x輸出,這應該是顯而易見。

確保您的所有變量都是不含特殊字符的整數。 (請注意,echo和視覺檢查不足以確定這一點,因爲並非所有的字符都是可打印或可區分的)。

+0

你先生是絕對的英雄!很棒的地方,謝謝! –

0

正如大家所說,這對我來說也是如此(GNU bash,版本3.2.57(1)-release(x86_64-apple-darwin16),MacOS Sierra 10.12.4)。

試試這個

if [ $immediate -lt $lowerBound -o $immediate -gt $upperBound ]; then 
    command 
fi 
+0

謝謝@theofanis。使用-o也更清潔!在其他機器上工作,這是最奇怪的部分。很高興它不是語法問題,但。再次感謝 –

+0

如果解決了您的問題,請將答案標記爲已接受。 – Theofanis