Escape-Characters
錯誤替換:heredoc / EOF 中沒有關閉“`”
我想列印一個男人風格的用法消息來描述這樣的輸出
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:我接受了引用的 here-document
<<'END_HELP'
的答案,但我最終不會將它用於這種完整的手動輸出,因為kusalananda確實建議編輯 1:(*供將來閱讀)*使用引用的 here-document的限制是防止
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 ;
在這裡,請注意作為錯誤來源的轉義反引號:
\`.'
反引號引入了命令替換。由於未引用 here-document,因此 shell 將對其進行解釋。由於命令替換沒有結束反引號,因此 shell 會抱怨。
要引用此處的文件,請使用
cat <<'END_HELP' something something help END_HELP
或者
cat <<\END_HELP something something help END_HELP
關於您對此問題解決的意見:
實用程序很少自己輸出完整的手冊,但可能會提供概要或基本使用資訊。這很少(如果有的話)是彩色的(因為它的輸出可能不會被定向到終端或尋呼機之類的
less
)。真正的手冊通常使用groff
或專門的手冊頁格式化程序排版,並且與程式碼完全分開處理。mandoc