Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Bash遍历多个数组_Arrays_Bash_Loops - Fatal编程技术网

Arrays Bash遍历多个数组

Arrays Bash遍历多个数组,arrays,bash,loops,Arrays,Bash,Loops,我定义了多个数组: array1=(el1 el2 el3 el4 el5) array2=(el10 el12 el14) array3=(el5 el4 el11 el8) 我需要遍历所有数组的所有元素。以下是我使用的语法: for j in {1..3} do for (( k = 0 ; k < ${#array$j[*]} ; k++ )) do #perform actions on array elements, refer to array

我定义了多个数组:

array1=(el1 el2 el3 el4 el5)
array2=(el10 el12 el14)
array3=(el5 el4 el11 el8)
我需要遍历所有数组的所有元素。以下是我使用的语法:

for j in {1..3}
do
    for (( k = 0 ; k < ${#array$j[*]} ; k++ ))
    do
        #perform actions on array elements, refer to array elements as "${array$j[$k]}"
    done
done
{1..3}中j的

做
对于((k=0;k<${#数组$j[*]};k++)
做
#对数组元素执行操作,将数组元素称为“${array$j[$k]}”
完成
完成
但是,上面的代码段失败并显示消息

k<${#数组$j[*]}:错误的替换和
${array$j[$k]}:替换错误

我的数组语法有什么问题?

您的脚本不正确

  • 首先,在3个数组中不应该用逗号分隔各个元素
  • 然后要使用数组名作为变量,需要使用间接寻址
  • 应采取以下措施:

    array1=(el1 el2 el3 el4 el5)
    array2=(el10 el12 el14)
    array3=(el5 el4 el11 el8)
    
    for j in {1..3}
    do
        n="array$j[@]"
        arr=("${!n}")
        for (( k = 0 ; k < ${#arr[@]} ; k++ ))
        do
            #perform actions on array elements, refer to array elements as "${array$j[$k]}"
            echo "Processing: ${arr[$k]}"
        done
    done
    
    这:

    打印此文件:

    ${array1[0]} is el1
    ${array1[1]} is el2
    ${array1[2]} is el3
    ${array1[3]} is el4
    ${array1[4]} is el5
    ${array2[0]} is el10
    ${array2[1]} is el12
    ${array2[2]} is el14
    ${array3[0]} is el5
    ${array3[1]} is el4
    ${array3[2]} is el11
    ${array3[3]} is el8
    

    如果不创建
    tmp_数组,可能有一些方法可以做到这一点,但是如果没有它,我就无法使间接寻址工作。

    您真的需要
    $j
    $k
    ,还是可以只为“${array1[@]}”${array2[@]}”${array3[@]}”中的值编写
    ;做完成了
    ?不幸的是,我完成了。数组的实际数量是11,每个数组中元素的操作需要是不同的。数组赋值的语法是错误的,不应该有任何逗号。抱歉,这只是我在写问题时犯的一个错误。我的脚本中的数组规范没有任何逗号。哈,我们只是在15秒内发布了相同的答案。伟大的头脑…!:-哦,那真的很有趣,我非常同意:)谢谢你——就是这样。数组定义中的逗号只是我在写问题时的一个输入错误。我的脚本中的数组规范没有任何逗号。使用间接解决了这个问题。如果这个答案帮助您解决您的问题,请考虑将其标记为“接受”,这样用户将来面对类似的问题将能够很容易地看到它。
    Processing: el1
    Processing: el2
    Processing: el3
    Processing: el4
    Processing: el5
    Processing: el10
    Processing: el12
    Processing: el14
    Processing: el5
    Processing: el4
    Processing: el11
    Processing: el8
    
    array1=(el1 el2 el3 el4 el5)
    array2=(el10 el12 el14)
    array3=(el5 el4 el11 el8)
    
    for j in {1..3} ; do
        # copy array$j to a new array called tmp_array:
        tmp_array_name="array$j[@]"
        tmp_array=("${!tmp_array_name}")
    
        # iterate over the keys of tmp_array:
        for k in "${!tmp_array[@]}" ; do
            echo "\${array$j[$k]} is ${tmp_array[k]}"
        done
    done
    
    ${array1[0]} is el1
    ${array1[1]} is el2
    ${array1[2]} is el3
    ${array1[3]} is el4
    ${array1[4]} is el5
    ${array2[0]} is el10
    ${array2[1]} is el12
    ${array2[2]} is el14
    ${array3[0]} is el5
    ${array3[1]} is el4
    ${array3[2]} is el11
    ${array3[3]} is el8