"[[0:找不到命令]";在巴什 [英] "[0: command not found" in Bash

查看:94
本文介绍了"[[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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆