How do I create a "here document" within a shell function?

Posted by BenU on Super User See other posts from Super User or by BenU
Published on 2012-11-24T19:54:41Z Indexed on 2012/11/24 23:07 UTC
Read the original article Hit count: 378

Filed under:
|
|
|
|

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!

© Super User or respective owner

Related posts about linux

Related posts about osx