5

Can someone explain why the end of the file is unexpected on line 49? (Line 49 is one line after the last line)

#!/bin/bash 

timeend=$(date -u +%H%M)
timestart=$(date --date "$timeend 30 minutes ago" -u +%H%M)
firsttime=0

while true
do
    if [[ $firsttime -eq 0 ]]; then
    time=$timestart
    increment=0
    fi
    if [[ $firsttime -ne true ]]; then
    increment=$(( $increment + 2 ))
    time=$(( $timestart + $increment ))
    fi
    if [[ $time -ge $timeend ]]; then
    break
    fi 

    gpnids << EOF
    RADFIL   = NEXRIII|CLT|TR0
    RADTIM   = "$time"
    TITLE    = 1/-2
    PANEL    = 0
    DEVICE   = gif|radar"$increment".gif|1280;1024|C
    CLEAR    = Y
    TEXT     = 1/2/2/hw
    COLORS   = 7
    WIND     =  
    LINE     =  
    CLRBAR   =  
    IMCBAR   = 5/v/LL/.005;.6/.4;.01
    GAREA    = dset
    MAP      = 24 + 23 + 1/1/2 + 14 + 15/1/2
    LATLON   = 0
    OUTPUT   = t

    $mapfil = lorvus.usg + hicnus.nws + hipona.nws + louhus.nws + loisus.nws
    run

    exit
    EOF
    firsttime=1

    gpend

 done
5
  • This question might be better asked on 'code review', another stack exchange site. Jun 19, 2014 at 16:19
  • Thanks for the tip, I added that site to my favorites and will turn to it for future scripting issues unless they might benefit the general user.
    – WxPilot
    Jun 19, 2014 at 16:25
  • @CharlesGreen No way. CR says clearly: "However, if your question is ... about ... Trouble-shooting, debugging, or understanding code snippets ... then your question is off-topic for this site". Btw., CR is a young beta site with hardly anyone wasting their time there.
    – maaartinus
    Jun 19, 2014 at 19:38
  • @maaartinus Crud - I thought since it said 'peer review' that it would be filled with people trying to figure out how to write code effectively. How about 'stack overflow'? Jun 19, 2014 at 22:00
  • 1
    @CharlesGreen It's just about improving code and code style, definitely not about finding bugs. SO or Linux might be a better fit, as there's nothing Ubuntu-specific in this question. IMHO it all should be a single site with content nicely separated by "site-tags" (this question should be site-tagged SO and Linux).
    – maaartinus
    Jun 19, 2014 at 22:09

4 Answers 4

12

You should also have gotten another error which is perhaps more informative:

/home/terdon/scripts/b.sh: line 49: warning: here-document at line 21 delimited by end-of-file (wanted `EOF')

/home/terdon/scripts/b.sh: line 50: syntax error: unexpected end of file

Your error is that you have spaces before the string that ends the heredoc. To take a simple example, this complains:

#!/bin/bash 

cat << EOF
   hello
   EOF

But this doesn't:

#!/bin/bash 

cat << EOF
   hello
EOF
3
  • That fixed it, I guess my assumption that whitespace is always a safe bet is wrong. Thanks!
    – WxPilot
    Jun 19, 2014 at 16:26
  • 2
    @WxPilot very wrong. There are many cases where whitespace is a problem in bash. For example foo = bar fails, as does foo= bar, in many cases bash will automatically split on whitespace etc. You should always avoid it unless absolutely necessary.
    – terdon
    Jun 19, 2014 at 16:30
  • I'll keep that in mind when I'm dealing with scripts
    – WxPilot
    Jun 19, 2014 at 16:37
11

I get two lines that should help you work out what's going on:

./test: line 48: warning: here-document at line 21 delimited by end-of-file (wanted `EOF')
./test: line 49: syntax error: unexpected end of file

Your heredoc (<< EOF) construction is incorrectly formed. It's whitespace sensitive so you either strip it back:

...
    command <<EOF
        ...
EOF

Or let it know you're tabbing it(and it must be a tab):

...
    command <<-EOF
        ...
    EOF

I prefer the second because it lets you structure the script a lot better... Something your script could already benefit from.

1
  • This "must be a tab" comment saved my sanity. I had copied a docker-entrypoint.sh over to my text editor which had somehow converted all of the tabs into spaces so the script itself was failing in a CI/CD pipeline when there wasn't anything wrong with it. Converting spaces into tabs solved my issue after I saw this comment, thanks.
    – Dascienz
    Oct 21, 2020 at 2:24
1

End of File warning

%>: line 49: warning: here-document at line 21 delimited by end-of-file (wanted 'EOF')

  • heredoc is looking for the delimiter (end tag), in this case EOF
  • it's never never recognized in your example because it is prefixed by spaces
  • the end of the actual file is reached without ever finding the delimiter; hence the warning

This can be addressed by removing the spaces, or as Terndon points out using tabs -- I didn't know this


Other

Another common error for end of file error that occurs deals with whitespace issues. Generally from copying code online formatted for Windows and running it in Linux.

This can be addressed by running dos2unix on the file to quickly convert those characters.

0

If you are using vim or vi try to use the command

:set list

You will be able to see spaces between the symbol $

Sometimes It's come in handy to figure out some unexpected behavior.
In this case delete the white spaces finished the job.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .