0

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.

4
  • There is no $i in your script, other than as the item being iterated over. Do you want $i rather than $1? Commented Feb 16, 2017 at 16:25
  • ...to be clear, this has nothing at all to do with ssh, or with heredocs. You'd have the exact same problem with any other command -- for i in "${my_arr[@]}"; do echo "$1"; done would fail in the same way. Commented Feb 16, 2017 at 16:26
  • BTW, use declare -p my_arr to print its contents unambiguously, to answer the question of whether the array is in fact correctly populated. Commented Feb 16, 2017 at 16:27
  • As a second aside -- EOF is only honored as the end of a heredoc if not indented. If your real file isn't indented, the use only a four-space indent in your question to accurately reflect that, rather than the eight-space indent given here. Commented Feb 16, 2017 at 16:32

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

using the EOF in the loop causes the file to end. Since you can use anything I've changed it to END. This resolves the issue.
@zinnadean, the string EOF does not cause the file to end, unless the file itself is enclosed in a larger heredoc using that same sigil. In short, that problem can only be caused if you've got extra context not included in your question.
0

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

2 Comments

It was an error when I transcribed it. It should be "$i".
Then your original code is, in and of itself, fine -- and your bug is something that only takes place in the context of code you didn't provide in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.