"[[0:找不到命令]";在巴什 [英] "[0: command not found" in Bash
本文介绍了"[[0:找不到命令]";在巴什的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在while循环中获取数组,并且也需要更新数组中的值.
I am trying to get the array in the while-loop and need to update the value in array too.
下面是我尝试的代码.我收到此错误[0: command not found
Below is my code what I have tried. I get this error [0: command not found
#!/bin/bash
i=0
while [$i -le "{#myarray[@]}" ]
do
echo "Welcome $i times"
i= $(($i+1)))
done
我该如何解决?
推荐答案
赋值中[
之后需要一个空格,在=
之前或之后都不需要空格. $(($i+1)))
会尝试执行((...))
表达式的输出,我确定这不是您想要的.另外,您在数组名称前缺少$
.
Need a space after [
and no space before or after =
in the assignment. $(($i+1)))
would try to execute the output of the ((...))
expression and I am sure that's not what you want. Also, you are missing a $
before the array name.
更正这些问题后,您的while循环将为:
With these things corrected, your while loop would be:
#!/bin/bash
i=0
while [ "$i" -le "${#myarray[@]}" ]
do
echo "Welcome $i times"
i=$((i + 1))
done
-
i=$((i + 1))
也可以写为((i++))
- 总是最好在
[ ... ]
中用双引号将变量引起来
- 通过 shellcheck 检查脚本-您可以在那里找到最基本的问题
i=$((i + 1))
can also be written as((i++))
- it is always better to enclose variables in double quotes inside
[ ... ]
- check your script through shellcheck - you can catch most basic issues there
- Why should there be a space after '[' and before ']' in Bash?
- How to use double or single brackets, parentheses, curly braces
- Command not found error in Bash variable assignment
- Using [ ] vs [[ ]] in a Bash if statement
另请参阅:
这篇关于"[[0:找不到命令]";在巴什的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文