문자열을 연결하여 기존 변수 이름을 형성하고 배열 래퍼 형식으로 작업

문자열을 연결하여 기존 변수 이름을 형성하고 배열 래퍼 형식으로 작업
#!/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}"


어쩌면 잊어버리고 st필요 indirect_var에 따라 업데이트해야 할 수도 있습니다.

또한 간접 변수에서 개수를 가져올 수 있는 방법이 없습니다.아니요이와 같은 구문은 echo "${!#indirect_var}"완벽하게 사용할 수 있습니다.

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

count_indirect_var="#$indirect_var"카운트를 얻기 위해 a를 정의할 수도 있습니다 .

행운을 빌어요! .

답변2

간접 주소 지정을 사용하는 경우 중괄호와 달러 기호 없이 변수 이름(및 인덱스도 가능)만 사용됩니다.

indirect_var=$st[@]  # line 4.

관련 정보