加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 管道heredoc的语法;这是便携式的?

发布时间:2020-12-15 16:48:54 所属栏目:安全 来源:网络整理
导读:我熟悉这个语法: cat EOF | cmdtextEOF 但只是发现bash允许我写: cat EOF |textEOFcmd (heredoc用作cmd1的输入)。这看起来像一个非常奇怪的语法。它是便携式的吗? 是的,POSIX标准允许这样。 According to the 2008 version: The here-document shall be
我熟悉这个语法:
cat << EOF | cmd
text
EOF

但只是发现bash允许我写:

cat << EOF |
text
EOF
cmd

(heredoc用作cmd1的输入)。这看起来像一个非常奇怪的语法。它是便携式的吗?

是的,POSIX标准允许这样。 According to the 2008 version:

The here-document shall be treated as a single word that begins after
the next <newline> and continues until there is a line containing only
the delimiter and a <newline>,with no <blank> characters in between.
Then the next here-document starts,if there is one.

并且在同一行中包括多个“here-documents”的此示例:

cat <<eof1; cat <<eof2
Hi,eof1
Helene.
eof2

所以没有问题做重定向或管道。你的例子类似这样的东西:

cat file |
cmd

而shell语法(在链接页面上的更进一步)包括这些定义:

pipe_sequence    :                             command
                 | pipe_sequence '|' linebreak command

newline_list     :              NEWLINE
                 | newline_list NEWLINE
                 ;
linebreak        : newline_list
                 | /* empty */

因此,管道符号后面可以是行尾,并且仍然被认为是管道的一部分。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读