I have two arrays that I want to loop in. I construct those properly and before going into for loop, I do echo them to be sure everything is ok with arrays. But when I run the script, it outputs an error:
l<=: syntax error: operand expected (error token is "<="
I consulted the mighty Google and I understood it suffers from the lack of the second variable, but I mentioned earlier I do echo the values and everything seems to be OK. Here is the snippet..
#!/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
The variables in the for loop are echoed correctly but for loop won't work.. where am I wrong?
#as gniourf_gniourf answered:
"... 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!"
meaning error output is displayed not at the beginning of the loop, but when k has a greater value than array's indices, pointing an index that array does not include...