AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 423392
Accepted
el-teedee
el-teedee
Asked: 2018-02-12 07:20:07 +0800 CST2018-02-12 07:20:07 +0800 CST 2018-02-12 07:20:07 +0800 CST

错误替换:heredoc / EOF 中没有关闭“`”

  • 772

我想打印一个男人风格的用法消息来描述这样的输出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.
为此,我执行了以下操作:

  1. 未加引号here-document,用于tput执行命令
  2. 通过转义反引号来防止“错误替换”错误
  3. 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 ;

在这里,请注意作为错误来源的转义反引号:

\`.'
escape-characters command-substitution
  • 2 2 个回答
  • 5858 Views

2 个回答

  • Voted
  1. Best Answer
    Kusalananda
    2018-02-12T07:26:18+08:002018-02-12T07:26:18+08:00

    反引号引入了命令替换。由于未引用 here-document,因此 shell 将对其进行解释。由于命令替换没有结束反引号,因此 shell 会抱怨。

    要引用此处的文档,请使用

    cat <<'END_HELP'
    something something help
    END_HELP
    

    或者

    cat <<\END_HELP
    something something help
    END_HELP
    

    关于您对此问题解决的意见:

    实用程序很少自己输出完整的手册,但可能会提供概要或基本使用信息。这很少(如果有的话)是彩色的(因为它的输出可能不会被定向到终端或寻呼机之类的less)。真正的手册通常使用groff或专门的手册页格式化程序排版mandoc,并且与代码完全分开处理。

    • 12
  2. ilkkachu
    2018-02-12T08:07:18+08:002018-02-12T08:07:18+08:00

    您想专门使用带有单引号/撇号的反引号/重音符号来引用某些内容吗?请不要,这种组合看起来很糟糕。在大多数字体中,反引号是倾斜的,而 (ASCII) 撇号是直的。这是我的浏览器显示您的手册页片段最后一行的方式:

    在此处输入图像描述

    如果你想使用比垂直 ASCII 引号更漂亮的引号,你可能应该使用U+2018和U+2019之类的东西。

    输出当然取决于你的字体,但我认为“this”看起来比“this”更好。

    • 4

相关问题

  • 如何在 Busybox 上获取设备状态报告?

  • 清除终端回滚缓冲区的最简单方法+一些更深入的解释?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    ssh 无法协商:“找不到匹配的密码”,正在拒绝 cbc

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    如何卸载内核模块“nvidia-drm”?

    • 13 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Wong Jia Hau ssh-add 返回:“连接代理时出错:没有这样的文件或目录” 2018-08-24 23:28:13 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST
  • Martin Hope
    Bagas Sanjaya 为什么 Linux 使用 LF 作为换行符? 2017-12-20 05:48:21 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve