Bash配列の間接アクセス

Bash配列の間接アクセス

次の間接的な操作を実行しようとしています。

host_1=(192.168.0.100 user1 pass1)
host_2=(192.168.0.101 user2 pass2)

hostlist=( "host_1" "host_2" )

for item in ${hostlist[@]}; do

current_host_ip=${!item[0]}
current_host_user=${!item[1]}
current_host_pass=${!item[2]}

echo "IP: $current_host_ip User: $current_host_user Pass: $current_host_pass"

done

この間接要求をどのように実行するかを理解しようとしているので、「hostlist」配列からホスト名を抽出し、間接要求を実行してホスト1 IP、ユーザー、およびパスを抽出する必要があります。ただし、これを試した場合は、最初の変数(IPのみ)を取得するか、その中にあるすべての変数(変数名の末尾に[@]を追加する場合)、空の結果、または数値を取得します。大きなバッチで。まず、host_1配列をcurrent_変数にコピーしてから(スクリプトがいくつかの操作を実行した後)、host_2変数を同じ変数current_に渡す方法を理解していません。

私の間違いを指摘してもらえますか?私はこれが問題に対する解決策だと思いますが、採用することはできません。

配列のすべての要素を間接的に返します。

ベストアンサー1

配列変数への名前参照を使用できます。

for item in "${hostlist[@]}"; do

  declare -n hostvar=$item
  current_host_ip=${hostvar[0]}
  current_host_user=${hostvar[1]}
  current_host_pass=${hostvar[2]}

  echo "IP: $current_host_ip User: $current_host_user Pass: $current_host_pass"
done

ここで、変数はarrayまたはという変数を意味しますhostvar$itemhost_1host_2

変数間接参照と配列値のコピーを使用します。

for item in "${hostlist[@]}"; do

    x=${item}[@]
    y=( "${!x}" )

    current_host_ip=${y[0]}
    current_host_user=${y[1]}
    current_host_pass=${y[2]}

    echo "IP: $current_host_ip User: $current_host_user Pass: $current_host_pass"
done

おすすめ記事