Syntax error: unexpected end of file

Shell Script Syntax Error

Operating System: Red Hat Linux
Click here for the solution without the preamble.

I had just written a 70 line shell script on my PC on notepad before copying it up on to the Linux Server so I could forgive myself for a typo and the resultant error. I googled the error and it indicated I had an opening " without a closing one. I scanned the file but did not see anything amiss. I set on a path to investigate.

Linux Shell Execute Permission

The other main suggestion was about the file permissions. However I had already fixed this by a chmod u+x ./

Linux Shell Script Debugging

Following a Shell Script Tutorial I tried adding some debugging

echo “starting”
set -v on
set -x on
…rest of script
set -v off
set -x off
echo “done”

This printed out the whole file however the echoed statement it did not get past the starting

Simplify The Script

Next step was to collapse the script down to half a dozen lines. It still failed in the same way with a syntax error: unexpected end of file.

if [ $# -ne 4 ]; then
echo “missing parameters – source domain, template domain, target domain, owner”
exit 127
echo “Got here”
exit 0

If you cannot see the problem it is invisible but still present

While digging deeper into shell script debugging I read a later section on the same page that referred to Windows Carriage Returns. By opening the file with vi and using the set list command I was able to see that every line was terminated by ^M.


Convert file from Windows to Unix format using dos2unix

I ran dos2unix ./

And problem solved. No more syntax error: unexpected end of file.

My script then worked first time after that, even the 70 line version.

So the moral of the story is.

Either do your editing entirely on Linux or always remember to convert dos2unix having copied the files on to the server if you are using low tech editors like notepad (as opposed to sophsiticated ones like vi :-)

Be Sociable, Share!

About Russell

Technologist and Internet Marketer
This entry was posted in Technology and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv badge