syntax error: unexpected end of file when editing code within here documents with emacs. working fine with nano

Viewed 219

my sys_info_script as edited with emacs:

#!/bin/bash

# Program to output a system information page

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

function report_uptime {
    cat <<- _EOF_
            <h2>System Uptime</h2>
            <pre>$(uptime)</pre>
            _EOF_
    return
}

function report_disk_space {
    cat <<- _EOF_
            <h2>Disk Space Utilization</h2>
            <pre>$(df -h)</pre>
            _EOF_
    return
}

function report_home_space {
    cat <<- _EOF_
            <h2>Home Space Utilization</h2>
            <pre>$(du -sh /home/*)</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_

when i execute the script if get an unexpected end of file `syntax-error

the same file as edited by nano:

#!/bin/bash

# Program to output a system information page

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

function report_uptime {
        cat <<- _EOF_
                <h2>System Uptime</h2>
                <pre>$(uptime)</pre>
                _EOF_
        return
}

function report_disk_space {
        cat <<- _EOF_
                <h2>Disk Space Utilization</h2>
                <pre>$(df -h)</pre>
                _EOF_
        return
}

function report_home_space {
        cat <<- _EOF_
                <h2>Home Space Utilization</h2>
                <pre>$(du -sh /home/*)</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_

this one runs fine

the sdiff results on the cat -A of the two file (< emacs-edit and > nano-edit):enter image description here

As you can see emacs's default space-indentation behavior is probably causing the trouble within here documents.

I'd like to continue to use emacs to edit shell scripts with here documents. How do I fix this issue?

Thanks!

1 Answers

Per the bash manual regarding here docs,

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.

You must therefore ensure that only tabs are used for such indentation, as bash does not support spaces for indentation in this situation.

The buffer-local indent-tabs-mode variable controls whether or not Emacs will use tabs in indentation, and then you also need to ensure that the buffer-local tab-width aligns with (in this case) sh-basic-offset so that your indentation will only be tabs, and not a mixture of tabs and spaces.

See also M-x customize-group RET sh-indentation RET

These could potentially be set as file-local variables, to ensure that other Emacs users wouldn't accidentally break this formatting.

Related