4

I have been trying to run the following script from the book Linux Command Line, but am getting an error unexpected end of file. I incrementally filled in each function and it seems like the issue is with the function report_home_space(). The script looks okay to me, though.

Any idea where the issue lies?

#!/bin/bash

# Program to output a system information page

declare -r TITLE="System Information Report for $HOSTNAME"
declare -r CURRENT_TIME=$(date)
declare -r TIMESTAMP="Generated $CURRENT_TIME, by $USER"

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

report_disk_space(){
        cat <<- _EOF_
                <H2>Disk Space Utilization</H2>
                <PRE>$(df -h)</PRE>
                _EOF_
        return
}

report_home_space(){
        cat <<- _EOF_
                <H2>Home Space Utilization</H2>
                <PRE>$(du -sh ~/*)</PRE>
                _EOF_
        return
}

cat << _EOF_ 
<HTML>
        <HEAD>
                <TITLE>$TITLE</TITLE>
        </HEAD>
        <BODY>
                <H1>$TITLE</H1>
                <P>$TIMESTAMP</P>
                $(report_uptime)
                $(report_disk_space)
                $(report_home_space)
        </BODY>
</HTML>
_EOF_
user2300040
  • 83
  • 1
  • 4

1 Answers1

3

The <<- form of here documents are very sensitive to leading whitespace. They only allow for tabs. If you accidentally used leading space or your editor expands tabs automatically, the here document would never find its closing token, and that would account for the error you see.

If you want to maintain your indentation, but want an alternative to the <<- notation, consider just using echo or printf, like so:

report() {
    printf '<H2>%s</H2>\n<PRE>%s</PRE>\n' "$1" "$2"
}

report_home_space() {
    report 'Home Space Utilization' "$(du -sh ~/*)"
}

# Other reports as above...
kojiro
  • 74,557
  • 19
  • 143
  • 201
  • Thank you! It indeed was a leading whitespace issue in the report_home_space() function. I removed the leading whitespace for all the lines in the here document for that function then made sure to only use tabs. – user2300040 Jan 05 '15 at 01:11