Arrays 在bash中循环时更改数组值
代码Arrays 在bash中循环时更改数组值,arrays,bash,Arrays,Bash,代码 SourceFolder[0]="" SourceFolder[1]="inbound2" SourceFolder[2]="inbound3" for i in "${!SourceFolder[@]}" do if [ -z "${SourceFolder[$i]}"]; then ${SourceFolder[$i]} = "TEST" fi done ${SourceFolder[$i]}=TEST-不工作 上面说 =:找不到命令 如何更
SourceFolder[0]=""
SourceFolder[1]="inbound2"
SourceFolder[2]="inbound3"
for i in "${!SourceFolder[@]}"
do
if [ -z "${SourceFolder[$i]}"]; then
${SourceFolder[$i]} = "TEST"
fi
done
${SourceFolder[$i]}=TEST-不工作
上面说
=:找不到命令
如何更改数组中当前循环索引的值?因为第一个空格=不被解释为赋值。有一个问题 顺便说一句${SourceFolder[$i]}计算数组元素,这不是您想要做的。例如,对于第一个,它是空字符串
替换为SourceFolder[$i]=,因为第一个空格=未解释为分配。有一个问题 顺便说一句${SourceFolder[$i]}计算数组元素,这不是您想要做的。例如,对于第一个,它是空字符串
替换为SourceFolder[$i]=您必须更改阵列中的索引编号:
ARRAYNAME[indexnumber]=value
好的,你有一个数组是:
array=(one two three)
您可以向脚本中添加计数以初始化和更改数组indexnumber的元素,例如:
#!/bin/bash
count=0
array=(one two three)
for i in ${array[@]}
do
echo "$i"
array[$count]="$i-indexnumber-is-$count"
count=$((count + 1))
echo $count
done
echo ${array[*]}
结果:
bash test-arr.sh
one
1
two
2
three
3
one-indexnumber-is-0 two-indexnumber-is-1 three-indexnumber-is-2
您必须更改数组中的indexnumber:
ARRAYNAME[indexnumber]=value
好的,你有一个数组是:
array=(one two three)
您可以向脚本中添加计数以初始化和更改数组indexnumber的元素,例如:
#!/bin/bash
count=0
array=(one two three)
for i in ${array[@]}
do
echo "$i"
array[$count]="$i-indexnumber-is-$count"
count=$((count + 1))
echo $count
done
echo ${array[*]}
结果:
bash test-arr.sh
one
1
two
2
three
3
one-indexnumber-is-0 two-indexnumber-is-1 three-indexnumber-is-2
您是否尝试过不使用空格:a=b而不是a=b?@cirosantilli相同的东西=测试:命令未找到建议的解决方案是否适合您?@devnull是的,该问题的解决方案有效。但在这种情况下,如果数组大小将改变,我必须用额外的循环来编辑代码,我不喜欢这就是我决定这样做的原因。你是否尝试过不使用空格:a=b而不是a=b?@cirosantilli same thing=TEST:command not Found建议的解决方案不适合你吗?@devnull是的,这个问题的解决办法是有效的。但在这种情况下,如果数组大小将改变,我必须用额外的循环来编辑代码,我不喜欢,这就是为什么我决定这样做的原因,这很有帮助!顺便说一句,它在if[-z${SourceFolder[$i]}]中有另一个错误;然后它说[:missing]“`为什么?再次注意你的空格。应该是[-z${SourceFolder[$i]}]这很有帮助!顺便说一句,它在if[-z${SourceFolder[$i]}]中有另一个错误;然后它说[:missing]“`为什么?再次注意你的空格。应该是[-z${SourceFolder[$i]}]