对于我当前的用例,我正在创建一个scp脚本,该脚本将日志文件从一台服务器复制到一台或多台其他服务器。
即
server1:/my/path1/log-files.* --> log_server1:/log/path1/server1
server1:/my/path2/log-files.* --> log_server2:/log/path/server1
server1:/my/path3/log-files.* --> log_server1:/log/path2/server1
我希望能够在bash(版本4)中使用Associationve arrays(arrays)进行日志文件配置,并通过将它们的名称放入索引数组中来循环所有A.数组。
但我对如何使用变量作为a.Array的名称来引用命名的a.Array感到困惑。
示例:
#!/bin/bash
# GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) from RedHat/CentOS 6.4
declare -A log_server1 log_server2
log_server1=([name]="ls1" [user]="user")
log_server2=([name]="ls2" [user]="user")
declare -A log1 log2 log3
log1=([log_server]="log_server1" [path]="/my/path1" [file]="log-files" [rpath]="/log/path1/server1")
log2=([log_server]="log_server2" [path]="/my/path2" [file]="log-files" [rpath]="/log/path/server1")
log3=([log_server]="log_server1" [path]="/my/path3" [file]="log-files" [rpath]="/log/path2/server1")
logs=(log1 log2 log3)
for log in ${logs[@]}
do
# How can I now refer to the A. Array by the name of "log1", etc ?
...
done
您可以使用间接展开,但它真的很难看!
#!/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
在我上面链接的参考手册部分,你会读到:
如果参数的第一个字符是感叹号(
!
),则引入了可变间接性级别。Bash使用由参数的其余部分组成的变量的值作为变量的名称;然后展开该变量,并在替换的其余部分使用该值,而不是参数本身的值。这就是所谓的间接扩张。这方面的例外是下面描述的${!prefix}
和${!name[@]}
的扩展。感叹号必须紧跟在左大括号后面才能引入间接性。
问题为什么不使用键log1
、log2
和log3
创建关联数组log_server
、path
、file
和rpath
?如:
#!/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
给出我自己的答案。我期待一些健康的批评m:-)然而,主要的问题是如何使用相同的关联数组,并以统一的方式在它们上循环。
关于如何实现这一目标的建议将不胜感激:
#!/bin/bash
# GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) from RedHat/CentOS 6.4
declare -A log_server1 log_server2
log_server1=([name]="ls1" [user]="user")
log_server2=([name]="ls2" [user]="user")
declare -A clog1 clog2 clog3
clog1=([log_server]="log_server1" [path]="/my/path1" [file]="log-files" [rpath]="/log/path1/server1/")
clog2=([log_server]="log_server2" [path]="/my/path2" [file]="log-files" [rpath]="/log/path/server1/")
clog3=([log_server]="log_server1" [path]="/my/path3" [file]="log-files" [rpath]="/log/path2/server1/")
for log in ${!clog*}
do
l_ls=$log[log_server] ; l_p=$log[path] ; l_f=$log[file] ; l_rp=$log[rpath]
l_ls=${!l_ls} ; l_p=${!l_p} ; l_f=${!l_f} ; l_rp=${!l_rp}
r_n=$l_ls[name] ; r_u=$l_ls[user]
r_n=${!r_n} ; r_u=${!r_u}
echo "Array $log:"
cmd=" scp ${l_p}/${l_f}* ${r_u}@${r_n}:${l_rp}"
echo "${cmd}"
done
结果:
$./bash-A-Array.sh
Array clog1:
scp /my/path1/log-files* user@ls1:/log/path1/server1/
Array clog2:
scp /my/path2/log-files* user@ls2:/log/path/server1/
Array clog3:
scp /my/path3/log-files* user@ls1:/log/path2/server1/