Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some of my here doc delimiters in bash script getting skipped

Very new to scripting. Working my way through "The Linux Command Line" by Shotts.

I keep running into an issue with some of the here doc delimiters being skipped.

I have report_uptime and report_disk_space commented out currently, but when I uncomment them some of my Here doc delimiters are being skipped.

Am I just missing something blaringly obvious breaking the script, or am I jacking up the here docs somehow?

#!/bin/bash

# Program to output a system information page

##### Constants

TITLE="System Information Report For $HOSTNAME"
CURRENT_TIME=$(date +"%x %r %Z")
TIME_STAMP="Generated $CURRENT_TIME, by $USER"

##### Functions

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 /home/*)</PRE>
#       _EOF_       
#   return
#}

##### HTML code

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_
like image 696
matches-malone Avatar asked Dec 12 '25 00:12

matches-malone


1 Answers

The Bash man page says this of here documents (emphasis added):

This type of redirection instructs the shell to read input from the current source until a line containing only [the delimiter] (with no trailing blanks) is seen. All of the lines read up to that point are then used as the standard input for a command.

[...]

If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing [the delimiter].

I reviewed the literal whitespace in your code by opening the question for editing. Supposing that the code presented accurately reflects your actual script, including whitespace, your problem is trailing spaces on the lines containing your here documents' closing delimiters.

like image 143
John Bollinger Avatar answered Dec 14 '25 16:12

John Bollinger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!