语法错误:使用 Bash 时需要操作数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13672022/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax error: operand expected when using Bash
提问by teutara
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:
我有两个我想循环的数组。我正确地构造了它们,在进入 for 循环之前,我会回显它们以确保数组一切正常。但是当我运行脚本时,它输出一个错误:
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?
for 循环中的变量正确回显,但 for 循环不起作用..我哪里错了?
#as gniourf_gniourf answered:
正如 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 将达到 ${#hitEnd[@]} 的值,而这正是当 hitEnd[k] 未定义并扩展为空字符串时!砰!”
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...
这意味着错误输出不显示在循环的开头,但是当 k 的值大于数组的索引时,指向数组不包含的索引...
采纳答案by gniourf_gniourf
That's because at some point ${hitEnd[k]}expands to nothing (it is undefined). I get the same error with ((l<=)). You should write your forloop as:
那是因为在某些时候${hitEnd[k]}扩展为空(它是未定义的)。我遇到同样的错误((l<=))。您应该将for循环编写为:
k=0
for ((l=${hitStart[0]};k<${#hitEnd[@]} && l<=${hitEnd[k]};l++)); do
so as to always have an index kthat corresponds to a defined field in the array ${hitEnd[@]}.
以便始终具有k与数组中定义的字段相对应的索引${hitEnd[@]}。
Also, instead of
另外,代替
k=$((k+1))
you can just write
你可以写
((++k))
Done!
完毕!
Your script revised using better modern bash practice:
您的脚本使用更好的现代 bash 实践进行了修改:
#!/bin/bash
k=0
#this loop is just for being sure array is loaded
while ((k<=${#hitEnd[@]})); do
    echo "hitEnd is: ${hitEnd[k]} and hitStart is: ${hitStart[k]}"
    # here outputs the values correct 
    ((++k))
done
k=0
for ((l=hitStart[0];k<${#hitEnd[@]} && l<=hitEnd[k];++l)); do
    ((++array[l]))
    ((++k))
done
Now, I'm not too sure the forloop does exactly what you want it to... Don't you mean this instead?
现在,我不太确定for循环完全按照您的意愿执行……您不是说这个吗?
#!/bin/bash
# define arrays hitStart[@] and hitEnd[@]...
# define array array[@]
#this loop is just for being sure array is loaded
for ((k=0;k<${#hitEnd[@]};++k)); do
    echo "hitEnd is: ${hitEnd[k]} and hitStart is: ${hitStart[k]}"
    # here outputs the values correct 
    ((++k))
done
for ((k=0;k<${#hitEnd[@]};++k)); do
    for ((l=hitStart[k];l<=hitEnd[k];++l)); do
        ((++array[l]))
    done
done
回答by sampson-chen
A bit bandaid-y, but you rewrite your for-loop into a while loop:
有点创可贴,但您将 for 循环重写为 while 循环:
l="${hitStart[k]}"
while [[ "$l" -le "${hitEnd[k]}" ]]; do
        let array[l]++      
        k=$((k+1))
        l=$((l+1))
done

