我想打印一个男士风格使用信息描述像这样的输出的 shell 函数man find
:
NAME
find - search for files in a directory hierarchy
SYNOPSIS
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]
DESCRIPTION
This manual page documents the GNU version of find. GNU find searches the directory tree rooted at each
given starting-point by evaluating the given expression from left to right, according to the rules of
precedence (see section OPERATORS), until the outcome is known (the left hand side is false for and opera‐
tions, true for or), at which point find moves on to the next file name. If no starting-point is speci‐
fied, `.' is assumed.
OPTIONS
我在 ` 字符上遇到错误消息。
以下简单脚本显示了错误:
~$ cat <<EOF
`.'
EOF
bash: bad substitution: no closing "`" in `.'
我认为heredoc
通过粘贴字符串来回显字符串是一种很酷的方法不必转义其内容,例如引号等......
我想我错了:/
有人可以解释一下这种行为吗?可以heredoc
接受 ` 字符吗?
编辑2: 我接受了答案此处引用文档 <<'END_HELP'
,但我最终不会将它用于这种完整的手动输出,因为善行做建议
编辑1:(供以后阅读)使用的限制此处引用文档是阻止tput
在 中使用here-document
。
为此,我执行了以下操作:
- 不加引号
here-document
,用于tput
要执行的命令 - 通过转义反引号来防止“错误替换”错误
tput
内 使用here-document
例子:
normal=$( tput sgr0 ) ;
bold=$(tput bold) ;
cat <<END_HELP # here-document not quoted
${bold}NAME${normal}
find - search for files in a directory hierarchy
${bold}SYNOPSIS${normal}
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]
${bold}DESCRIPTION${normal}
This manual page documents the GNU version of find. GNU find searches the directory tree rooted at each
given starting-point by evaluating the given expression from left to right, according to the rules of
precedence (see section OPERATORS), until the outcome is known (the left hand side is false for and opera‐
tions, true for or), at which point find moves on to the next file name. If no starting-point is speci‐
fied, \`.' is assumed.
END_HELP
unset normal ;
unset bold ;
在这里,请注意作为错误来源的转义反引号:
\`.'
答案1
反引号引入了命令替换。由于此处的文档没有被引用,因此这将由 shell 解释。 shell 会抱怨,因为命令替换没有结束反引号。
要引用此处文档,请使用
cat <<'END_HELP'
something something help
END_HELP
或者
cat <<\END_HELP
something something help
END_HELP
关于您对解决此问题的意见:
实用程序很少自己输出完整的手册,但可能会提供概要或基本使用信息。这很少(如果有的话)被着色(因为它的输出可能不会定向到终端或寻呼机,例如less
)。真正的手册通常是使用groff
专用的手册页格式化程序来排版的,例如mandoc
并且与代码完全分开处理。