2015-11-20 58 views
0

我遇到了一個小問題,下面的代碼片斷,我不知道爲什麼。給出的誤差是(線表示):Bash腳本操作數預計

* 2:語法錯誤:操作數預期(錯誤標記爲 「* 2」)

while [[ $numberServers -gt $newindex ]]; do 
    serverPort=$((9001+$(($newindex*2))))  <--- This line 
    clientPort=$(($serverPort+1)) 
    newindex=$(($newindex+1)) 
    localhostport=$((serverPort-2)) 

    string=$(($string,localhost:$(($serverPort-2)))) 
... 

任何幫助,將不勝感激。

+0

聽起來像是'$ newindex'沒有您所期望的價值。它有什麼價值? –

+0

它看起來好像'$ newindex'是空的。在公式中使用它之前,您應該測試它是否有價值。另外,括號太多! 'serverPort = $((9001 + $ newindex * 2))'應該完全一樣。同樣,'newindex = $(($ newindex + 1))'可以用'((newindex ++))'代替。 – ghoti

+1

你也在濫用最後一行的算術表達式;大概你想追加字符串'localhost:$(($ serverPort-2))'到'string'的末尾。 – chepner

回答

2

的問題是,可變newindex是空的,所以表達成爲:

$((9001+$((*2)))) 

檢查newindex初始化。

例子:

$ echo $((9001+$(($newindex*2)))) 
bash: *2: syntax error: operand expected (error token is "*2") 

$ newindex=4 

$ echo $((9001+$(($newindex*2)))) 
9009 
+0

謝謝!真的很愚蠢。但是現在我又遇到了另一個錯誤?第26行:,localhost:9001:語法錯誤:期望操作數(錯誤標記爲「,localhost:9001」) – fierynot

+0

@CAFC_Sam沒問題..看起來像一個語法錯誤..很難說沒有看到有問題的行,即第26行.. – heemayl

+0

第26行只是 string = $(($ string,localhost:$(($ serverPort-2)))) – fierynot