文字列を連結して既存の変数名を形成し、配列ラッパー形式で作業する

文字列を連結して既存の変数名を形成し、配列ラッパー形式で作業する
#!/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}[@]"あなたの説明は私が確信するほど明確ではありません。

ただし、変数を1つだけ変更すると、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}"

count_indirect_var="#$indirect_var"カウントを取得するためにaを定義することもできます。

頑張ってください! 。

おすすめ記事