简体   繁体   中英

Populate array to ssh in bash

Just some background, I have a file with 1000 servers in it new line delimted. I have to read them to an array the run about 5 commands over SSH. I have been using heredoc notation but that seems to fail. 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.

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.

That is to say: ssh "$1" needs to instead by ssh "$i" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM