Here the document is the unexpected end of the file.

I try to run the following script from the Linux Command Line book, but I get an error message with an unexpected file termination. I fill in each function step by step, and it seems that the problem is with the report_home_space () function. However, the script looks good.

Any idea where the problem is?

#!/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_
+4
source share
1 answer

The form of <<-the documents here is very sensitive to leading spaces. They only allow tabs. If you accidentally used the leading space or your editor automatically expands the tabs, here the document will never find its final token, and this will take into account the error you see.

, <<-, echo printf, :

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...
+3

Source: https://habr.com/ru/post/1569927/


All Articles