Syntax Error: unexpected end of file — Bash script

98,700

The diagnostic "unexpected end of file" is a hint that you have some unmatched or unterminated opening syntactical construct (if w/o fi, do w/o done, opening brackets w/o the associated closing one, opening but unterminated quotes, etc.). The line number pointing to the end of the script is not helpful in this case, beyond saying to inspect your syntactical constructs; the error may be anywhere in your code. You have to check that.

Share:
98,700

Related videos on Youtube

Scott Pearce
Author by

Scott Pearce

Updated on September 18, 2022

Comments

  • Scott Pearce
    Scott Pearce over 1 year

    I am trying to create an spritz app. Everything was working fine, but since yesterday I keep getting this error:

    ./spritz: line 176: syntax error: unexpected end of file

    I have checked the script file and everything seems perfect. I am confused, I have an if statement at last and it looks correct! Here is the last portion:

    #checks if speed is 150
    157 if [[ $2 -eq 150 ]];
    158 then
    159 starttime=$SECONDS
    160      FS=$'\n'
    161      for j in `grep --color=always -iP '\b[^aeiou\s]*[aeiou][^aeiou\s]*\K[aeiou]' $1`;
    162      do
    163            #Reads the text file in the centre of the screen
    164            echo "                                                    ___________________"
    165            echo "                                                             $j";
    166            echo "                                                    ___________________"
    167            echo "                                                                               Speed 150 wpm"
    168            sleep  0.9;
    169            clear;
    170       done
    171 endtime=$(($SECONDS - $starttime))
    172            echo "You read $words_read words in $endtime seconds!"
    173       exit 8
    174 fi
    

    What could cause that error?

    • Scott - Слава Україні
      Scott - Слава Україні about 9 years
      (0) It might have been more useful to show us a diff from the last version that worked. … … … … … … … … Some observations (that probably don’t relate to your current, specific problem): (1) I don’t know of any circumstance where you need an unescaped ; (semicolon) at the end of a line.  You can delete the semicolons at the ends of lines 157, 161, 165, 168, and 169.  (Or you can leave them in; I guess it’s a question of style.)  … (Cont’d)
    • Scott - Слава Україні
      Scott - Слава Україні about 9 years
      (Cont’d) …  (2) You should always quote shell variables unless you have a good reason not to and you’re sure you know what you’re doing; e.g., "$1", "$2", "$SECONDS", and "$starttime".  (3) It’s easier to read and debug code that’s indented properly.  (4) Why are you setting FS and then not using it?  (5) `…` can be written $(…).  (6) $(($SECONDS - $starttime)) can equivalently be written $((SECONDS - starttime)).  Again, this is basically an issue of style.  (7) Why are you printing "$words_read" in the loop when you aren’t modifying it in the loop?
    • Scott - Слава Україні
      Scott - Слава Україні about 9 years
      Here’s a debugging approach: Try deleting lines 157-174 and running the script again.   If you still get the error (probably reported as being at line 157 or 158), then you know the problem isn’t in lines 157-174, and you have to look further back (so, repeat the process until the error goes away).  … (Cont’d)
    • Janis
      Janis about 9 years
      Remarks on a few of Scott's comments: (ad 2) if you are sure what you are doing then (in the posted code) only "$1" needs quoting, (ad 4) probably he meant IFS (and not FS)?
  • Scott Pearce
    Scott Pearce about 9 years
    Thanks for the reply. I have found the error and fixed it!
  • naive
    naive about 5 years
    --the error may be anywhere in your code. Created account just to upvote for this advice.