Bash syntax error: unexpected end of file

Here’s the code:

bash 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

  • file.sh is with CRLF line terminators.
bash code
dos2unix file.sh
[ad type=”banner”]

then the problem will be fixed.

  • You can install dos2unix in ubuntu with this:
bash code
sudo apt-get install dos2unix
bash code
die () { test -n "$@" && echo "$@"; exit 1 }

  • Terminate bodies of single-line functions with semicolon

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

 die () { test -n "$@" && echo "$@"; exit 1 }
[ad type=”banner”]
  • To make the dumb parser:
bash code
die () { test -n "$@" && echo "$@"; exit 1; }

  • We needed on cygwin :-
bash code
 export SHELLOPTS
set -o igncr
[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: