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
.
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.
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”.
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.
Problem is spaces before closing ENDSSH
. Take out all the leading spaces before ENDSSH
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With