简体   繁体   English

语法错误:使用Bash时预期的操作数

[英]Syntax error: operand expected when using Bash

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. 我有两个我想要循环的数组。我正确地构造它们,然后在进入for循环之前,我确实回应它们以确保数组的一切正常。 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? 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的值大于数组的索引时,指向该数组不包含的索引...

That's because at some point ${hitEnd[k]} expands to nothing (it is undefined). 那是因为在某些时候${hitEnd[k]}扩展为${hitEnd[k]} (未定义)。 I get the same error with ((l<=)) . 我用((l<=))得到了同样的错误。 You should write your for loop as: 您应该将for循环写为:

k=0
for ((l=${hitStart[0]};k<${#hitEnd[@]} && l<=${hitEnd[k]};l++)); do

so as to always have an index k that 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 for loop 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

A bit bandaid-y, but you rewrite your for-loop into a while loop: 有点bandaid-y,但你将for循环重写为while循环:

l="${hitStart[k]}"
while [[ "$l" -le "${hitEnd[k]}" ]]; do
        let array[l]++      
        k=$((k+1))
        l=$((l+1))
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 bash:语法错误:预期的操作数(错误标记为“-”) - bash: syntax error: operand expected (error token is “-”) Bash脚本错误:“语法错误:预期操作数(错误令牌为” / backup”)” - Bash Script error: “syntax error: operand expected (error token is ”/backup“)” Bash:语法错误操作数应为“=”,在 for 循环中的赋值语句中 - Bash: syntax error operand expected “=”, in assignment statement in a for loop Bash-错误:语法错误:预期操作数(错误令牌为“ testdir / .hidd1 /”) - Bash - Error: Syntax error: operand expected (error token is “testdir/.hidd1/”) 语法错误:预期操作数(错误标记为“+”) - Syntax error: operand expected (error token is "+) 使用case语句时,bash语法错误 - bash syntax error when using case statement Bash:使用Wgrib2时,语法错误接近意外的令牌`(&#39; - Bash: Syntax Error Near Unexpected Token `(' When Using Wgrib2 bash:使用((“ $ var” ==“ string”))进行比较时出现语法错误 - bash: syntax error using (( “$var” == “string” )) for comparisons 提到的语法错误是在这种情况下使用Bash的项目 - The mentioned syntax error was in this case item using Bash SLES 12语法错误:“ =〜”意外的运算符/操作数 - SLES 12 syntax error: '=~' unexpected operator/operand
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM