This question already has an answer here:
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
.
The
ENDSSH
marker has to be at the left margin:When using
<<- ENDSSH
you can indent the marker, but it must be indented with Tab characters, not spaces.Problem is spaces before closing
ENDSSH
. Take out all the leading spaces beforeENDSSH
.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.