Forgive me for this is a very simple script in Bash. Here's the code:
#!/bin/bash
# june 2011
if [ $# -lt 3 -o $# -gt 3 ]; then
echo "Error... Usage: $0 host database username"
exit 0
fi
after running sh file.sh:
syntax error: unexpected end of file
I think file.sh is with CRLF line terminators.
run
then the problem will be fixed.
You can install dos2unix in ubuntu with this:
Make sure the name of the directory in which the .sh file is present does not have a space character. e.g: Say if it is in a folder called 'New Folder', you're bound to come across the error that you've cited. Instead just name it as 'New_Folder'. I hope this helps.
I just cut-and-pasted your example into a file; it ran fine under bash. I don't see any problems with it.
For good measure you may want to ensure it ends with a newline, though bash shouldn't care. (It runs for me both with and without the final newline.)
You'll sometimes see strange errors if you've accidentally embedded a control character in the file. Since it's a short script, try creating a new script by pasting it from your question here on StackOverflow, or by simply re-typing it.
What version of bash are you using? (
bash --version
)Good luck!
i also just got this error message by using the wrong syntax in an
if
clauseelse if
(syntax error: unexpected end of file)elif
(correct syntax)i debugged it by commenting bits out until it worked
I got this answer from this similar problem on StackOverflow
Open the file in Vim and try
:set fileformat=unix
Thanks @lemongrassnginger
Apparently, some versions of the shell can also emit this message when the final line of your script lacks a newline.