bash 如何通过命令管理此处的文档并将结果捕获到变量中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2128949/
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
How to pipe a here-document through a command and capture the result into a variable?
提问by Mark Renouf
Right now this outputs the value I need on stdout. How can I capture it into a variable so I can use it in the rest of the script?
现在这会输出我在标准输出上需要的值。我如何将它捕获到一个变量中,以便我可以在脚本的其余部分中使用它?
Requirements:
要求:
- The script needs to be all in one file.
- I'd prefer not to write any temp files, if possible.
- 该脚本需要全部在一个文件中。
- 如果可能,我不想写任何临时文件。
.
.
#!/bin/bash
cat << EOF | xsltproc - ../pom.xml | tail -1
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"><xsl:value-of select="/project/version"/></xsl:template>
</xsl:stylesheet>
EOF
采纳答案by Mark Renouf
This seems to work (based on Ignacio's answer). By using a subshell the here-document is correctly piped into xsltproc while still being passed through tail after.
这似乎有效(基于 Ignacio 的回答)。通过使用子shell,here-document被正确地传送到xsltproc,同时仍然通过tail after。
VERSION=$((xsltproc - ../pom.xml | tail -1) << EOF
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"><xsl:value-of select="/project/version"/></xsl:template>
</xsl:stylesheet>
EOF
)
回答by Ignacio Vazquez-Abrams
The cat ... |isn't necessary.
该cat ... |是没有必要的。
foo=$(sed 's/-/_/g' << EOF
1-2
3-4
EOF
)
回答by mikeserv
I've been playing around with heredocsfor a week or two. Here's an excerpt from my answer to Is there a way to get actual(uninterpreted) shell arguments in a function or script?at Unix Stack Exchange that might help illustrate their use a little for your case:
我已经玩heredocs了一两个星期了。这是我对Is there a way to get actual(uninterpreted) shell arguments in a function or script? 的回答的摘录?在 Unix Stack Exchange 上,这可能有助于说明它们对您的情况的使用:
... EXCERPT:...
... 摘录:...
Probably you noticed the difference between the two heredocs in the second example. The heredoc EOFterminator within the function is unquoted, while the one fed to read is quoted with single quotes. In this way the shell is instructed to perform expansion on the heredoc with an unquoted terminator, but not to do so when its terminator is quoted. It doesn't break when expanding the unquoted heredoc in the function because the value of the variable it expands is already set as a quoted string and it doesn't parse it twice.
可能你注意到了第二个例子中两个 heredocs 之间的区别。函数中的 heredoc EOF终止符没有被引用,而被读入的那个终结符被单引号引用。通过这种方式,shell 被指示对具有不带引号的终止符的 heredoc 执行扩展,但在其终止符被引用时不这样做。在函数中扩展未加引号的 heredoc 时它不会中断,因为它扩展的变量的值已经设置为带引号的字符串,并且不会对其进行两次解析。
Probably what you want to do involves piping your Windows path from the output of one command into the input of another dynamically. Command substitution within a heredoc makes this possible:
可能您想要做的涉及将 Windows 路径从一个命令的输出动态传输到另一个命令的输入。Heredoc 中的命令替换使这成为可能:
% _stupid_mspath_fix() {
> sed -e 's@\@/@g' -e 's@\(.\):\(.*\)@/drive/@' <<_EOF_
>>
>> _EOF_
> }
% read -r _stupid_mspath_arg <<'_EOF_'
> c:\some\stupid\windows\place
> _EOF_
% _stupid_mspath_fix ${_stupid_mspath_arg}
/drive/c/some/stupid/windows/place
% read -r _second_stupid_mspath_arg <<_EOF_
> $(printf ${_stupid_mspath_arg})
> _EOF_
% _stupid_mspath_fix ${_second_stupid_mspath_arg}
/drive/c/some/stupid/windows/place
So basically if you can reliably output the backslashes from some application (I used printf above), then running that command within $(...) and enclosing that within an unquoted heredoc passed to another application that can reliably accept the backslashes as input (such as read and sed above) will bypass the shell's parsing of your backslashes altogether. Whether or not the applications can handle the backslashes as input/output is something you'll have to find out for yourself.
所以基本上,如果您可以可靠地从某个应用程序输出反斜杠(我在上面使用了 printf),然后在 $(...) 中运行该命令并将其包含在传递给另一个可以可靠地接受反斜杠作为输入的另一个应用程序的未加引号的 heredoc 中(例如上面的 read 和 sed )将完全绕过 shell 对反斜杠的解析。应用程序是否可以将反斜杠作为输入/输出处理,这是您必须自己找出的问题。
-Mike
-麦克风

