You can use indirect expansion, but it's really ugly!
#!/bin/bash
declare -A log_server1=([name]="ls1" [user]="user")
declare -A log_server2=([name]="ls2" [user]="user")
declare -A log1=([log_server]="log_server1" [path]="/my/path1" [file]="log-files" [rpath]="/log/path1/server1")
declare -A log2=([log_server]="log_server2" [path]="/my/path2" [file]="log-files" [rpath]="/log/path/server1")
declare -A log3=([log_server]="log_server1" [path]="/my/path3" [file]="log-files" [rpath]="/log/path2/server1")
logs=( log1 log2 log3 )
for log in "${logs[@]}"; do
l_ls=$log[log_server]
l_p=$log[path]
l_f=$log[file]
l_rp=$log[rpath]
echo "array $log:"
echo " log_server => ${!l_ls}"
echo " path => ${!l_p}"
echo " file => ${!l_f}"
echo " rpath => ${!l_rp}"
done
In the reference manual section I linked above, you'll read:
If the first character of parameter is an exclamation point (
!
), a level of variable indirection is introduced. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of${!prefix}
and${!name[@]}
described below. The exclamation point must immediately follow the left brace in order to introduce indirection.
Question. Why don't you, instead, create associative arrays log_server
, path
, file
and rpath
with keys log1
, log2
and log3
? as in:
#!/bin/bash
declare -A log_server1=([name]="ls1" [user]="user")
declare -A log_server2=([name]="ls2" [user]="user")
declare -A log_server path file rpath
log_server[log1]="log_server1"
path[log1]="/my/path1"
file[log1]="log-files"
rpath[log1]="/log/path1/server1"
log_server[log2]="log_server2"
path[log2]="/my/path2"
file[log2]="log-files"
rpath[log2]="/log/path/server1"
log_server[log3]="log_server3"
path[log3]="/my/path3"
file[log3]="log-files"
rpath[log3]="/log/path2/server1"
for log in "${!log_server[@]}"; do
echo "log server $log:"
echo " log_server => ${log_server[$log]}"
echo " path => ${path[$log]}"
echo " file => ${file[$log]}"
echo " rpath => ${rpath[$log]}"
done