zinnadean
zinnadean

Reputation: 33

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.

Upvotes: 0

Views: 277

Answers (2)

Charles Duffy
Charles Duffy

Reputation: 295363

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".

Upvotes: 0

arco444
arco444

Reputation: 22821

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

Upvotes: 2

Related Questions