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

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

我的 sys_info_script 使用 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_

当我执行脚本时,如果出现 unexpected end of file `syntax-error

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_

这个运行良好

两个文件(nano-edit)cat -Asdiff 结果:

如您所见,emacs 的默认 space-缩进行为可能导致了 here documents.

中的问题

我想继续使用 emacs 和 here documents 一起编辑 shell 脚本。我该如何解决这个问题?

谢谢!

根据有关此处文档的 bash 手册,

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.

因此,您必须确保 选项卡用于此类 缩进,因为 bash 不支持缩进空格 情况。

buffer-local indent-tabs-mode 变量控制是否 Emacs 会使用制表符进行缩进,然后你还需要确保 buffer-local tab-width 对齐(在本例中) sh-basic-offset 这样你的缩进就只会是制表符,而不是 制表符和空格的混合体。

另见 M-x customize-group RET sh-indentation RET

这些可能被设置为文件局部变量,以确保其他 Emacs 用户不会意外破坏这种格式。