Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm working my way through William Shotts Jr.'s great The Linux Command Line on my Mac OSX 10.7.5 system. 90% of the linux that Shotts covers is close enough to Darwin that I can figure out or GTEM to figure out what's going on. I've made it to chapter 27 on "Writing Shell Scripts" and am getting hung up creating "here files" within a function.

I get an syntax error: unexpected end of file error when I include the following function:

report_uptime () {
  cat <<- _EOF_
    <H2>System Uptime</H2>
    <PRE>$(uptime)</PRE>
    _EOF_
  return
}

The error goes away if I use the following function placeholder:

report_uptime () {
  return
}

Also, elsewhere in the script, outside of a function I use the cat << _EOF_ format to create a "here file" with no trouble:

cat << _EOF_
<HTML>
      <HEAD>
            <TITLE>$TITLE</TITLE>
      </HEAD>
      <BODY>
            <H1>$TITLE</H1>
            <P>$TIME_STAMP</P>
            $(report_uptime)
            $(report_disk_space)
            $(report_home_space)
      </BODY>
</HTML>
_EOF_

If anyone has any idea what I'm doing wrong I would be grateful!

share|improve this question
2  
The EOF token must be the first thing on the line, without any leading white spaces. –  Ярослав Рахматуллин Nov 24 '12 at 19:58
    
That worked for me, @Ярослав_Рахматуллин. I don't think that the [-] is an option for Darwin's here files. cat << _EOF_ not cat <<- _EOF_ –  BenU Nov 24 '12 at 20:32
    
that's a shame. –  Ярослав Рахматуллин Nov 24 '12 at 22:07

1 Answer 1

up vote 2 down vote accepted

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.

If that does not work as documented, try moving the EOF token to the very beginning of the line (remove all white spaces).

share|improve this answer
    
This should work regardless of the system, unless the bash version is broken on your system. –  Ярослав Рахматуллин Nov 24 '12 at 20:15
    
Thanks @Ярослав_Рахматуллин Though adding the - to << didn't solve my problem, removing the whitespace before the _EOF_ did. Perhaps "here documents" with cat << works in Darwin but not cat <<-. It's a shame since <<- is so useful. Move your comment into your answer and I will accept it. Addendum: bash may in fact be broken. Possibly broken by me, possibly Mac's Darwin's limitations... –  BenU Nov 24 '12 at 20:17
1  
@BenU, note that the man page is very specific here: tab characters not whitespace –  glenn jackman Nov 25 '12 at 4:41
    
@glennjackman Using spaces instead of tabs could indeed be the source of my problem. I've arranged my text editor to use two spaces instead of tab by default. Thanks! –  BenU Nov 25 '12 at 4:47

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.