[英]Populate array to ssh in bash
Just some background, I have a file with 1000 servers in it new line delimted. 只是有些背景,我有一个文件包含1000台服务器,其中一行换了一行。 I have to read them to an array the run about 5 commands over SSH. 我必须将它们读入数组,通过SSH运行约5条命令。 I have been using heredoc notation but that seems to fail. 我一直在使用heredoc表示法,但这似乎失败了。 Currently I get an error saying the host isn't recognized. 目前,我收到一条错误消息,指出无法识别主机。
IFS='\n' read -d '' -r -a my_arr < file
my_arr=()
for i in "${my_arr[@]}"; do
ssh "$1" bash -s << "EOF"
echo "making back up of some file"
cp /path/to/file /path/to/file.bak
exit
EOF
done
I get output that lists the first server but then all the ones in the array as well. 我得到的输出列出了第一个服务器,但随后列出了阵列中的所有服务器。 I know that I am missing a redirect for STDIN that causes this. 我知道我错过了导致这种情况的STDIN重定向。
Thanks for the help. 谢谢您的帮助。
Do you need an array? 您需要数组吗? What is wrong with: 出什么问题了:
while read -r host
do
ssh "$host" bash -s << "EOF"
echo "making back up of some file"
cp /path/to/file /path/to/file.bak
EOF
done < file
To be clear -- the problem here, and the only problem present in the code actually included in your question , is that you're using $1
inside your loop, whereas you specified $i
as the variable that contains the entry being iterated over on each invocation of the loop. 明确地说-这里的问题以及问题中实际包含的代码中存在的唯一问题是,您在循环内使用了$1
,而您将$i
指定$i
包含要在其上进行迭代的条目的变量循环的每次调用。
That is to say: ssh "$1"
needs to instead by ssh "$i"
. 这就是说: ssh "$1"
需要代替ssh "$i"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.