我有兩個我想要循環的數組。我正確地構造這些數組,並且在進入for循環之前,我會迴應它們以確保數組中的一切都正常。 但是當我運行該腳本,它輸出一個錯誤:語法錯誤:使用Bash時預期的操作數
l<=: syntax error: operand expected (error token is "<="
我諮詢了強大的谷歌,我的理解是因爲缺乏第二個變量的遭遇,但我前面提到的我做呼應的價值觀和一切似乎沒問題。下面是摘錄..
#!/bin/bash
k=0
#this loop is just for being sure array is loaded
while [[ $k -le ${#hitEnd[@]} ]]
do
echo "hitEnd is: ${hitEnd[k]} and hitStart is: ${hitStart[k]}"
# here outputs the values correct
k=$((k+1))
done
k=0
for ((l=${hitStart[k]};l<=${hitEnd[k]};l++)) ; do //this is error line..
let array[l]++
k=$((k+1))
done
的變量在for循環正確迴應,但for循環將不會工作。我在哪裏錯了?
#作爲gniourf_gniourf回答:
"... At some point, k will reach the value ${#hitEnd[@]}, and this is exactly when hitEnd[k] is not defined and expands to an empty string! Bang!"
意味着誤差輸出是在循環開始時不顯示,但是,當k具有比陣列的指數爲更大的值,指向一個索引陣列不包括...
感謝gniourf_gniourf,會盡量在後一點.. – teutara
我還是不明白清楚爲什麼我在問題中提到的情況不起作用,但是謝謝你的努力.. – teutara
我以爲你會理解它!注意變量'k'總是如何遞增的,並且記住'for'循環中的測試部分(即'l <= hitEnd [k]'部分)在每次通過時被評估! –