Bash syntax error: unexpected end of file

Here’s the code:

[pastacode lang=”bash” manual=”%23!%2Fbin%2Fbash%0A%23%20june%202011%0A%0Aif%20%5B%20%24%23%20-lt%203%20-o%20%24%23%20-gt%203%20%5D%3B%20then%0A%20%20%20echo%20%22Error…%20Usage%3A%20%240%20host%20database%20username%22%0A%20%20%20exit%200%0AFi%0A” message=”bash code” highlight=”” provider=”manual”/]
  • after running sh file.sh:
  • syntax error: unexpected end of file

  • file.sh is with CRLF line terminators.
[pastacode lang=”bash” manual=”dos2unix%20file.sh%0A” message=”bash code” highlight=”” provider=”manual”/] [ad type=”banner”]

then the problem will be fixed.

  • You can install dos2unix in ubuntu with this:
[pastacode lang=”markup” manual=”sudo%20apt-get%20install%20dos2unix” message=”bash code” highlight=”” provider=”manual”/] [pastacode lang=”bash” manual=”die%20()%20%7B%20test%20-n%20%22%24%40%22%20%26%26%20echo%20%22%24%40%22%3B%20exit%201%20%7D%0A” message=”bash code” highlight=”” provider=”manual”/]

  • Terminate bodies of single-line functions with semicolon

I.e. this innocent-looking snippet will cause the same error:

[pastacode lang=”bash” manual=”%20die%20()%20%7B%20test%20-n%20%22%24%40%22%20%26%26%20echo%20%22%24%40%22%3B%20exit%201%20%7D%0A” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]
  • To make the dumb parser:
[pastacode lang=”bash” manual=”die%20()%20%7B%20test%20-n%20%22%24%40%22%20%26%26%20echo%20%22%24%40%22%3B%20exit%201%3B%20%7D%0A” message=”bash code” highlight=”” provider=”manual”/]

  • We needed on cygwin :-
[pastacode lang=”bash” manual=”%20export%20SHELLOPTS%0A%20set%20-o%20igncr%0A” message=”bash code” highlight=”” provider=”manual”/] [ad type=”banner”]

in .bash_profile . This way we didn’t need to run unix2dos

  • 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’.

Categorized in: