3

I need to run a script remotely. I am using the following shell script

    for server in $servers
     do
     LOCAL_VAR=<some_value>
     ssh $server <<EOF
      command1 $LOCAL_VAR
      command2..
      ..
      exit
      EOF
    done

bash shows unexpected end of file syntax error. The rest of the code works fine if I remove this block. Can you please tell me the correct way of executing a script remotely.

2
  • Is your EOF indented? It should not be. Commented Oct 8, 2012 at 22:58
  • Ya it was. sputnick's solution worked perfectly Commented Oct 11, 2012 at 22:23

3 Answers 3

2

If you want to put indentation like that in your here-doc, you should add a - like the following code :

for server in $servers
do
    LOCAL_VAR=<some_value>
    ssh $server <<-EOF
    command1 $LOCAL_VAR
    command2..
    ..
    exit
    EOF
done

Take care when you copy paste this, sometimes you can have surprises with tabs or spaces.

Sign up to request clarification or add additional context in comments.

Comments

1

Your EOF to close the heredoc must not have any leading spaces. Bash does not think it has reached the end of your string before finding the end of the script.

http://tldp.org/LDP/abs/html/here-docs.html

The closing limit string, on the final line of a here document, must start in the first character position. There can be no leading whitespace. Trailing whitespace after the limit string likewise causes unexpected behavior. The whitespace prevents the limit string from being recognized.

1 Comment

Interesting. I didn't know that.
1

The solution is to have the literal label in column 1 the script. tl;dr No indenting for the literal label (EOF)

Comments

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.