15

I get the following error that is flagging on the last line of my code (which is empty):

syntax error: unexpected end of file

I can't figure out why it's saying this. I'm simply trying to use a here-doc for an ssh connection:

#!/bin/sh

connectToServer() {
   ssh -t root@$1 <<- ENDSSH
      echo "Connected to server!"
   ENDSSH
}

connectToServer $1

What's wrong with this code?

EDIT

Thanks to those of you who helped me to troubleshoot this. There were a couple of things wrong with my script; I was using spaces on the line:

echo "Connected to server" 

instead of tab characters. I was also including spaces before the closing ENDSSH which was causing the EOF. These changes were a part of my problem, but the final thing that resolved it was removing an additional space character that appeared AFTER my closing ENDSSH.

nullByteMe
  • 6,141
  • 13
  • 62
  • 99

3 Answers3

18

Problem is spaces before closing ENDSSH. Take out all the leading spaces before ENDSSH.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I just tried this and the error is the same. :-/ – nullByteMe Nov 14 '13 at 19:29
  • I reproduced same error by putting spaces before closing `ENDSSSH` It goes away when I stripped all leading spaces. – anubhava Nov 14 '13 at 19:33
  • 1
    I piped my script to `hexdump -C` and found that it wasn't a leading space or any characters before `ENDSSH` it was a `0a` or newline character following `ENDSSH`. I stripped that away and now it works. Thanks for helping me to find this! – nullByteMe Nov 14 '13 at 19:38
  • 1
    Do you mean `0d`? That's a CR, which means you edited the file on Windows and forgot to convert the CRLF to LF when moving to Unix. – Barmar Nov 14 '13 at 19:40
  • 1
    @Barmar good catch, I just looked at the output from hexdump and it wasn't the `0a` it was an additional space `20` after the closing `ENDSSH` – nullByteMe Nov 14 '13 at 19:44
10

The ENDSSH marker has to be at the left margin:

connectToServer() {
   ssh -t root@$1 << ENDSSH
      echo "Connected to server!"
ENDSSH
}

When using <<- ENDSSH you can indent the marker, but it must be indented with Tab characters, not spaces.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    I just tried this and on my line where I have `echo "Connected to server!" I removed all spaces and used tabs. I also removed the spaces from the closing `ENDSSH` and I still have the same error message. – nullByteMe Nov 14 '13 at 19:31
2

When using the <<- operator, only leading tabs are stripped from the here document and the line containing the marker. You appear to be indenting the closing marker with spaces, so that line appears to be part of the here document, and since the here document never closes, you reach the end of the file while parsing it.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • 2
    Isn't this the same as what anubhava and I answered? – Barmar Nov 14 '13 at 19:33
  • It wasn't when I was started typing. (I think I missed your comment in the last sentence of your answer, though). – chepner Nov 14 '13 at 19:35
  • Note that even with a leading tab before the ENDSSH I have the same issue on Mac. Fix was to not tab in front of it (even though <<- should allow it). – Joshua Jun 07 '22 at 17:59
  • Are you sure you had *just* a leading tab, or that your editor wasn't turning the tab into an equivalent number of spaces? This isn't something that would vary between versions of `bash`, which would be the only factor using macOS might introduce. – chepner Jun 07 '22 at 20:14