连接字符串以形成现有变量名称并在数组封装格式中工作

连接字符串以形成现有变量名称并在数组封装格式中工作
#!/bin/bash
mat_1=(ServerAB ServerFR ServerPE ServerAM ServerHU)
st="mat_1";
indirect_var='${'${st}'[@]}'

#(Please, see the "--Desired Ouput Section--" in comments)

#----- What is Hapenning now at output ----
echo Values of "mat_1 ": ${mat_1[@]}
echo Indirect value of "mat_1": ${!indirect_var}

# echo Indirect value of "mat_1": ${!indirect_var}  ##output> ${mat_1[@]}
# But it is not recognized as a real ${mat_1[@]}.

# -- What actually have ----
for (( i=0; i < ${#mat_1[@]}; i++ )) #I would like just only make 
                                  #that loop accepts that 
                                  #string 'mat_1' and operate
                                  #normally as if I were typed 
                                  # ${#mat_1[@]} , like
                                  # ${#'string'[@]}, working all 
                                  # together as a real array declare
                                  # ${#mat_1[@]}, and I may re-utilize
                                  # this loop and make a function where
                                  #I pass--> function-name $1, where $1 is
                                  # an string, and this string already
                                  # exist above , it will interpret as
                                  # an existing array


do
echo ${mat_1[i]};
done

#And I would like those strings 
#that are part of the name of existing 
#variables , will be treated as an input
# and this loop works. I will show What I have done,
# what I have reached, and what I expect to have.
# Thank you!


#------What I expect works-----

#for (( i=0; i < ${#$st[@]}; i++ ))  #I would like $st works like 'mat_1'
                                  #and this loop can be run without 
                                  #problems
#do
#   echo ${$st[i]};
#done

#--- Actual Output ------------
#$ ./matrix.sh 
# Values of mat_1 : ServerAB ServerFR ServerPE ServerAM ServerHU
# ./matrix.sh: line 8: ${mat_1[@]}: invalid variable name
# ServerAB
# ServerFR
# ServerPE

#--- Desired Output ----------

#$ ./matrix.sh 
# Values of mat_1 : ServerAB ServerFR ServerPE ServerAM ServerHU
# Indirect Value of mat_1: ServerAB ServerFR ServerPE ServerAM ServerHU
# ServerAB
# ServerFR
# ServerPE
# ServerAM
# ServerHU

“嗨,朋友们,我想要一些想法来实现以下目标”。

  • 我有许多现有的“数组变量”,我希望通过“连接字符串”在 for 循环中调用它们,以形成这些“现有数组名称”。但是在上面的脚本中,我只使用 01 数组“var_mat1” 。我只需要 01 数组的工作......
    • 现有“数组名称”的示例:

      var_mat1=( ".." ".." ".." )
      var_mat2=( ".." ".." ".." )
           .
           .
           .
      var_matN=( ".." ".." ".." )
      

答案1

在我看来,您在使用间接变量方面遇到了困难。

要访问名为 a 的数组变量:

a=(one two t33 f44)

您需要一个间接变量,该变量仅包含您在构造中编写的内容${...}。要获取 的值,${a[2]}您需要将间接变量设置为:

indirect=a[2]

然后,使用它:

$ echo "<${!indirect}>"
<t33>

我相信你应该检查一下里面有什么indirect_var='${'${st}'[@]}'

也许:indirect_var="${st}[@]"你的描述不够清楚,我无法确定。

但请注意,单独更改变量st不会访问其他变量/值。你必须indirect_var在它起作用之前改变它的值。

试试这个脚本:

#!/bin/bash
mat_1=(ServerAB ServerFR ServerPE ServerAM ServerHU)
mat_2=(SeAB SeFR SePE SeAM SeHU)

st="mat_1";
#indirect_var='${'${st}'[@]}'
indirect_var="${st}[@]"
#(Please, see the "--Desired Ouput Section--" in comments)

#----- What is Hapenning now at output ----
echo "Values of mat_1        : ${mat_1[@]}"
echo "Indirect value of mat_1: ${!indirect_var}"

st=mat_2
echo "This WONT work"
echo "Values of mat_2        : ${mat_2[@]}"
echo "Indirect value of mat_2: ${!indirect_var}"

st=mat_2
indirect_var="${st}[@]"
echo "This DOES work"
echo "Values of mat_2        : ${mat_2[@]}"
echo "Indirect value of mat_2: ${!indirect_var}"


也许您应该忘记stindirect_var根据需要进行更新。

另外,没有办法从间接变量中获取计数,有不是像这样的语法echo "${!#indirect_var}"但你可以完美地使用:

indirect_var="#mat_1[@]"
echo "${!indirect_var}"

也许定义 acount_indirect_var="#$indirect_var"来获取计数。

祝你好运!。

答案2

使用间接寻址时,仅使用变量的名称(可能还有索引),而不使用大括号和美元符号。

indirect_var=$st[@]  # line 4.

相关内容