How do I create a "here document" within a shell function?
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!
Solution 1:
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).