用于传递定界符的多行语法;这是便携式的吗?
我熟悉这种语法:
cmd1 << EOF | cmd2
text
EOF
但刚刚发现 bash 允许我编写:
cmd1 << EOF |
text
EOF
cmd2
(heredoc 用作 cmd1 的输入,cmd1 的输出通过管道传输到 cmd2)。这看起来是一个非常奇怪的语法。它是便携式的吗?
I'm familiar with this syntax:
cmd1 << EOF | cmd2
text
EOF
but just discovered that bash allows me to write:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,POSIX 标准允许这样做。 根据2008年版本:
并在同一行中包含多个“此处文档”的示例:
因此,进行重定向或管道是没有问题的。您的示例类似于以下内容:
shell 语法(链接页面的下方)包含以下定义:
因此管道符号后面可以跟有行尾,并且仍然被视为管道的一部分。
Yes, the POSIX standard allows this. According to the 2008 version:
And includes this example of multiple "here-documents" in the same line:
So there is no problem doing redirections or pipes. Your example is similar to something like this:
And the shell grammar (further down on the linked page) includes these definitions:
So a pipe symbol can be followed by an end-of-line and still be considered part of a pipeline.
是的,它在 POSIX shell 语法中。您还可以同一命令有多个here-doc(其他一些示例使用两个
cat
调用,但这也有效):这是人为的(使用2这里-docs for stdin),但如果您考虑为不同的文件描述符提供输入,它立即有意义。
还可以完全丢弃
猫
。为什么不让此处文档直接可供cmd
使用: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
cat
invocations, but this works as well):This is contrived (using 2 here-docs for stdin), but if you think of providing input for different file descriptors it immediately makes sense.
There's also the possibility to drop the
cat
entirely. Why not make the here-document directly available tocmd
:嗯,我想是的,根据 POSIX 模式下 bash 的测试:
Hmm, I suppose yes, according to the test in bash in POSIX mode:
嗨,请检查一下
问候
Hi, check this, for example
regards