bash 脚本中的一些文档定界符被跳过
Some of my here doc delimiters in bash script getting skipped
对脚本编写非常陌生。通过 Shotts 的 "The Linux Command Line" 我的工作方式。
我一直 运行 遇到一个问题,这里的一些文档分隔符被跳过了。
我目前已将 report_uptime 和 report_disk_space 注释掉,但是当我取消注释它们时,我的 Here doc 分隔符被跳过了。
我只是遗漏了一些明显破坏脚本的东西,还是我以某种方式抬高了此处的文档?
#!/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_
Bash 手册页说明了此处文档的内容(强调已添加):
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].
我打开问题进行编辑,检查了您代码中的文字空白。假设提供的代码准确地反映了您的实际脚本,包括空格,您的问题是包含此处文档的结束分隔符的行中的尾随空格。
对脚本编写非常陌生。通过 Shotts 的 "The Linux Command Line" 我的工作方式。
我一直 运行 遇到一个问题,这里的一些文档分隔符被跳过了。
我目前已将 report_uptime 和 report_disk_space 注释掉,但是当我取消注释它们时,我的 Here doc 分隔符被跳过了。
我只是遗漏了一些明显破坏脚本的东西,还是我以某种方式抬高了此处的文档?
#!/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_
Bash 手册页说明了此处文档的内容(强调已添加):
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].
我打开问题进行编辑,检查了您代码中的文字空白。假设提供的代码准确地反映了您的实际脚本,包括空格,您的问题是包含此处文档的结束分隔符的行中的尾随空格。