Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when using a bash here-doc "unexpected end of file" [duplicate]

Tags:

bash

ssh

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.

like image 753
hax0r_n_code Avatar asked Nov 14 '13 19:11

hax0r_n_code


People also ask

How fix Unexpected end of file in Linux?

An Unexpected end of file error in a Bash script usually occurs when you there is a mismatched structure somewhere in the script. If you forget to close your quotes, or you forget to terminate an if statement, while loop, etc, then you will run into the error when you try to execute your Bash script.

How do you end a file in Bash?

You can also use the “exit” clause to exit the bash script while stating 1 with it at run time. So, open the same file and update your code as we have done before. The only change is “exit 1” instead of “exit” or “exit 0”. Save your code and quit the editor via “Ctrl+S” and “Ctrl+X”.

How do you end a file in shell script?

To end a shell script and set its exit status, use the exit command. Give exit the exit status that your script should have. If it has no explicit status, it will exit with the status of the last command run.


1 Answers

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

like image 151
anubhava Avatar answered Sep 23 '22 03:09

anubhava