Bash Syntax error: unexpected end of file

Sorry this is a very simple script in Bash. Here is 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

+80
syntax bash
Jun 16 2018-11-11T00:
source share
17 answers

I think file.sh with CRLF line terminators.

run

 dos2unix file.sh 

then the problem will be fixed.

You can install dos2unix on ubuntu as follows:

 sudo apt-get install dos2unix 
+112
Jun 16 '11 at 2:50
source share

One more thing to check (just happened to me):

  • complete single line function bodies with semicolons

those. this innocent looking fragment will cause the same error:

 die () { test -n "$@" && echo "$@"; exit 1 } 

To make a dumb parser happy:

 die () { test -n "$@" && echo "$@"; exit 1; } 
+97
Jul 08 '14 at 15:37
source share

I also got this error message using the wrong syntax in the if clause

  • else if (syntax error: unexpected end of file)
  • elif (correct syntax)

i debugs it by commenting out bits until it works

+29
Nov 26 '15 at 13:36
source share

an unblocked sentence if => fi will also raise this value

tip: use trap for debugging if your script is huge ...

eg. set -x trap read debug ...

+12
Aug 28 '17 at 1:00
source share

on cygwin I need: -

  export SHELLOPTS set -o igncr 

in .bash_profile. So I did not need to run unix2dos

+6
Jun 17 '13 at 10:14
source share

I got this answer from https://stackoverflow.com/a/3/3128/

Open the file in Vim and try

:set fileformat=unix

Convert eh line endings to unix ending and see if this solves the issue. If you are editing in Vim, enter the command: set fileformat = unix and save the file. Several other editors have the ability to convert a string such as Notepad ++ or Atom

Thanks @lemongrassnginger

+6
Mar 12 '18 at 1:25
source share

So I found this post and the answers did not help me, but I could understand why it gave me an error. I had

 cat > temp.txt < EOF some content EOF 

The problem was that I copied the above code to be in a function and unintentionally fixed the code. You must ensure that the latest EOF is not enabled.

+5
Aug 23 '16 at 19:49
source share

I managed to cut and paste your code into a file, and it worked correctly. if you execute it as follows:

Your file "file.sh":

 #!/bin/bash # june 2011 if [ $# -lt 3 -o $# -gt 3 ]; then echo "Error... Usage: $0 host database username" exit 0 fi 

Team:

 $ ./file.sh arg1 arg2 arg3 

Note that "file.sh" must be executed:

 $ chmod +x file.sh 

You can get this b / c error from how you make input (with pipe, carrots, etc.). You can also try splitting the condition into two:

 if [ $# -lt 3 ] || [ $# -gt 3 ]; then echo "Error... Usage: $0 host database username" exit 0 fi 

Or, since you are using bash , you can use the built-in syntax:

 if [[ $# -lt 3 || $# -gt 3 ]]; then echo "Error... Usage: $0 host database username" exit 0 fi 

And finally, you could, of course, just check to see if 3 arguments were provided (clean, supports POSIX shell compatibility):

 if [ $# -ne 3 ]; then echo "Error... Usage: $0 host database username" exit 0 fi 
+1
Jun 16 2018-11-11T00:
source share

I had a problem when I wrote "if - fi" on one line:

if [ -f ~/.git-completion.bash ]; then. ~/.git-completion.bash fi

Write multi-line solved my problem:

if [ -f ~/.git-completion.bash ]; then. ~/.git-completion.bash fi

+1
Aug 28 '18 at 7:16
source share

I just cut and pasted your example into a file; it worked fine under bash. I do not see any problems with this.

For a good measure, you can make sure that it ends with a newline, although bash doesn't care. (It works for me both with the end of a new line and without it.)

Sometimes you see strange errors if you accidentally enter a control character in a file. Since this is a short script, try creating a new script by pasting it from your question here in StackOverflow or simply retyping it.

What version of bash are you using? ( bash --version )

Good luck

0
Jun 16 2018-11-11T00:
source share

Make sure that the name of the directory in which the .sh file is present does not have a space. for example: Tell me if it is located in the "New Folder" folder, you will certainly encounter the error you cited. Instead, just name it "New_Folder". Hope this helps.

0
03 Feb '16 at 4:44
source share

Apparently, some versions of the shell may also give this message when there is no new line in the last line of your script.

0
Sep 21 '16 at 3:17
source share

In Ubuntu:

 $ gedit ~/.profile 

Then File -> Save as and install end line on Unix/Linux

0
Mar 13 '18 at 8:12
source share

I know I'm too late for the party. Hope this can help someone.

Check your .bashrc file. Maybe rename or move it.

Discussion here: Unable to find simple bash script

0
Apr 19 '19 at 17:09 on
source share

This happened to me when I tried to call a function using paren, for example

 run() { echo hello } run() 

should be:

 run() { echo hello } run 
0
May 7, '19 at 6:50
source share

For windows:

In my case, I worked on Windows, and I got the same error when running autoconf.

  • I just open the configure.ac file with my NOTEPAD ++ IDE.
  • Then I converted the EOL to Windows (CR LF) File as follows:

    EDIT → EOL CONVERSION → WINDOWS (CR LF)

0
Jun 20 '19 at 20:13
source share

For people using MacOS:

If you received a file in Windows format and want to run on MacOS and see this error, run these commands.

 brew install dos2unix sh <file.sh> 
0
Jul 18 '19 at 22:15
source share



All Articles