Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
share|improve this question
    
This question might be better asked on 'code review', another stack exchange site. –  Charles Green Jun 19 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 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 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'? –  Charles Green Jun 19 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 at 22:09

2 Answers 2

up vote 6 down vote accepted

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
share|improve this answer
    
That fixed it, I guess my assumption that whitespace is always a safe bet is wrong. Thanks! –  WxPilot Jun 19 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 at 16:30
    
I'll keep that in mind when I'm dealing with scripts –  WxPilot Jun 19 at 16:37

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.