bash 管道传递的多行语法;这是便携的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7046381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:52:44  来源:igfitidea点击:

Multiline syntax for piping a heredoc; is this portable?

bashshellpipeshheredoc

提问by William Pursell

I'm familiar with this syntax:

我熟悉这种语法:

cmd1 << EOF | cmd2
text
EOF

but just discovered that bash allows me to write:

但刚刚发现 bash 允许我写:

cmd1 << EOF |
text
EOF
cmd2

(the heredoc is used as input to cmd1, and the output of cmd1 is piped to cmd2). This seems like a very odd syntax. Is it portable?

(heredoc 用作 cmd1 的输入,cmd1 的输出通过管道传输到 cmd2)。这似乎是一个非常奇怪的语法。便携吗?

采纳答案by Ned Deily

Yes, the POSIX standard allows this. According to the 2008 version:

是的,POSIX 标准允许这样做。 根据 2008 版:

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-document 应被视为一个单词,它在下一个之后开始<newline>,一直持续到有一行只包含分隔符和 a <newline>,中间没有<blank>字符。然后下一个 here-document 开始,如果有的话。

And includes this example of multiple "here-documents" in the same line:

并在同一行中包含多个“此处文档”的示例:

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

So there is no problem doing redirections or pipes. Your example is similar to something like this:

所以做重定向或管道没有问题。您的示例类似于以下内容:

cat file |
cmd

And the shell grammar (further down on the linked page) includes these definitions:

shell 语法(在链接页面的下方)包括以下定义:

pipe_sequence    :                             command
                 | pipe_sequence '|' linebreak command

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

So a pipe symbol can be followed by an end-of-line and still be considered part of a pipeline.

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

回答by Jens

Yes it's in the POSIX shell grammar. You can also have more than one here-doc for the same command(some other examples use two catinvocations, but this works as well):

是的,它在 POSIX shell 语法中。对于同一个命令,您还可以有多个 here-doc(其他一些示例使用两个cat调用,但这也适用):

cat <<EOF1 <<EOF2
first here-doc
EOF1
second here-doc
EOF2

This is contrived (using 2 here-docs for stdin), but if you think of providing input for different file descriptors it immediately makes sense.

这是人为的(将 2 个 here-docs 用于标准输入),但是如果您考虑为不同的文件描述符提供输入,它立即就有意义了。

There's also the possibility to drop the catentirely. Why not make the here-document directly available to cmd:

也有可能完全删除cat. 为什么不直接将 here-document 提供给cmd

cmd << EOF
input
here
EOF

回答by TMS

Hmm, I suppose yes, according to the test in bash in POSIX mode:

嗯,我想是的,根据 bash 在 POSIX 模式下的测试:

$ bash --posix
$ cat <<EOF |
> ahoj
> nazdar
> EOF
> sed 's/a/b/'
bhoj
nbzdar

回答by buc

Hi, check this, for example

嗨,检查这个,例如

#!/bin/sh
( base32 -d | base64 -d )<<ENDOFTEXT
KNDWW42DNNSHS5ZXPJCG4MSVM5MVQVT2JFCTK3DELBFDCY2IIJYGE2JUJNHWS22LINVHQMCMNVFD
CWJQIIZVUV2JOVNEOVJLINTW6PIK
ENDOFTEXT

regards

问候