【问题标题】:read array name from STDIN and iterating through the array values从 STDIN 读取数组名称并遍历数组值
【发布时间】:2013-08-09 20:29:21
【问题描述】:

我有许多数组,但只想遍历通过 STDIN 输入的数组 我的代码看起来像

ARR1=(a b c)
ARR2=(d e f)
ARR3=(g h i)

read array_name

for i in ${array_name[@]}
do
echo "i is $i"
done

现在,如果我输入 ARR1 作为输入,则不会打印 ARR1 值。有人可以帮忙吗

【问题讨论】:

    标签: arrays shell


    【解决方案1】:

    你可以写:

    ARR1=(a b c)
    ARR2=(d e f)
    ARR3=(g h i)
    
    read array_name
    
    array_name="${array_name}[@]"    # append '[@]' to array_name
    
    for i in "${!array_name}" ; do   # use indirection to expand e.g. "${ARR1[@]}"
        echo "i is $i"
    done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-30
      • 2020-10-31
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-02
      相关资源
      最近更新 更多