LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-31-2018, 07:50 PM   #1
jinlei
LQ Newbie
 
Registered: Aug 2018
Posts: 3

Rep: Reputation: Disabled
syntax error: unexpected end of file


Hopefully this is not a silly question. I am just repeating an example from the book <The Linux Command Line> Fourth Internet Edition by William Shotts. Taking the example on page 423, I always come across this problem. Can anyone help to figure it out?

Code:
#!/bin/bash
#Program to output a system information page
TITLE="System Information Report For $HOSTNAME"
CURRENT_TIME="$(date +"%x %r %Z")"
TIMESTAMP="Generated $CURRENT_TIME, by $USER"

report_home_space (){
    if [[ "$(id -u)" -eq 0 ]]; then
        cat <<- _EOF_
             <H2>Home Space Utilization (All Users)</H2>
             <PRE>$(du -sh /home/*)</PRE>
             _EOF_
    else
        cat <<- _EOF_
             <H2>Home Space Utilization ($USER)</H2>
             <PRE>$(du -sh $HOME)</PRE>
             _EOF_
    fi
    return
}

cat << _EOF_
<HTML>
    <HEAD>
        <TITLE>$TITLE</TITLE>
    </HEAD>
    <BODY>
            <H1>$TITLE</H1>
            <P>$TIMESTAMP</P>
            $(report_home_space)
    </BODY>
</HTML>
_EOF_
The problem is gone, if I shorten the above code as below:
Code:
#!/bin/bash
#Program to output a system information page
TITLE="System Information Report For $HOSTNAME"
CURRENT_TIME="$(date +"%x %r %Z")"
TIMESTAMP="Generated $CURRENT_TIME, by $USER"

report_home_space (){
    echo "Function report_home_space executed."
    return
}

cat << _EOF_
<HTML>
    <HEAD>
        <TITLE>$TITLE</TITLE>
    </HEAD>
    <BODY>
            <H1>$TITLE</H1>
            <P>$TIMESTAMP</P>
            $(report_home_space)
    </BODY>
</HTML>
_EOF_
To me, the only difference between the two pieces of codes is: there are two extra pairs of "_EOF_" in the first piece. But I might miss something obvious. Thank you!
 
Old 09-01-2018, 01:34 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,725

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Code:
cat <<- _EOF_
What is that hyphen doing there in the first two cat statements?

Use set -x to see what bash is doing...
Code:
#!/bin/bash
#Program to output a system information page
set -x
...
 
Old 09-01-2018, 01:42 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,679

Rep: Reputation: 5893Reputation: 5893Reputation: 5893Reputation: 5893Reputation: 5893Reputation: 5893Reputation: 5893Reputation: 5893Reputation: 5893Reputation: 5893Reputation: 5893
The eof has to at the beginning of the line. No spaces.
 
1 members found this post helpful.
Old 09-01-2018, 02:45 PM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
I think the - allows leading TABs.
And I think that characters ( and ) need to be escaped in order to satisfy the parser:
\(All Users\) and \($USER\)
 
Old 09-01-2018, 03:09 PM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,725

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by MadeInGermany View Post
I think the - allows leading TABs.
I see that is true...did not know that. From man bash -- Here Documents section:
Code:
If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter.  This allows here-documents within shell scripts to be indented in a natural fashion.
...so that should allow the not-left-flush ending _EOF_ to work IF the leading whitespace is tab(s), but apparently not if it is spaces...

That said, I'm pretty sure that there should be no spaces between the << or <<- and the word (in this case the word is _EOF_, but it can be any string). Try removing the space there.

(confirmed...setting the leading spaces to tabs still threw the error. Removing the space after the << and <<- allowed the script to run.)

Last edited by scasey; 09-01-2018 at 03:11 PM.
 
1 members found this post helpful.
Old 09-01-2018, 03:44 PM   #6
jinlei
LQ Newbie
 
Registered: Aug 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
The eof has to at the beginning of the line. No spaces.
Thank you very much! It works! Accurately, only the second "_EOF_" within each pair must stay at the beginning of a new line. The codes below works:

Code:
#!/bin/bash
#Program to output a system information page
TITLE="System Information Report For $HOSTNAME"
CURRENT_TIME="$(date +"%x %r %Z")"
TIMESTAMP="Generated $CURRENT_TIME, by $USER"

report_home_space (){
    if [[ "$(id -u)" -eq 0 ]]; then
        cat <<- _EOF_
             <H2>Home Space Utilization (All Users)</H2>
             <PRE>$(du -sh /home/*)</PRE>
_EOF_
    else
        cat <<- _EOF_
             <H2>Home Space Utilization ($USER)</H2>
             <PRE>$(du -sh $HOME)</PRE>
_EOF_
    fi
    return
}

cat << _EOF_
<HTML>
    <HEAD>
        <TITLE>$TITLE</TITLE>
    </HEAD>
    <BODY>
            <H1>$TITLE</H1>
            <P>$TIMESTAMP</P>
            $(report_home_space)
    </BODY>
</HTML>
_EOF_
 
Old 09-01-2018, 03:49 PM   #7
jinlei
LQ Newbie
 
Registered: Aug 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
I see that is true...did not know that. From man bash -- Here Documents section:
Code:
If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter.  This allows here-documents within shell scripts to be indented in a natural fashion.
...so that should allow the not-left-flush ending _EOF_ to work IF the leading whitespace is tab(s), but apparently not if it is spaces...

That said, I'm pretty sure that there should be no spaces between the << or <<- and the word (in this case the word is _EOF_, but it can be any string). Try removing the space there.

(confirmed...setting the leading spaces to tabs still threw the error. Removing the space after the << and <<- allowed the script to run.)
Thank you! You are also right. After I changed spaces as tabs, everything works.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax error, unexpected end of file rahilhm@gmail.com Linux - Newbie 6 08-05-2018 03:33 PM
Syntax Error: Unexpected End of File ntbluez Linux - Server 7 02-06-2016 04:21 AM
[SOLVED] syntax error: unexpected end of file Revenge7 Linux - Newbie 2 03-19-2015 04:35 AM
Syntax Error: Unexpected File End Ripowal Programming 13 09-20-2011 12:44 AM
syntax error: unexpected end of file ygdrazil Linux - Newbie 3 07-23-2009 05:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration