Bash

連接字元串以形成現有變數名稱並在數組附件格式中工作

  • April 12, 2021
#!/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=( ".." ".." ".." )
    

在我看來,您很難使用間接變數。

要訪問名為 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"來獲得計數。

祝你好運!。

引用自:https://unix.stackexchange.com/questions/644745