如何管这里一个文档通过一个命令,并捕获结果到一个变量? [英] How to pipe a here-document through a command and capture the result into a variable?

查看:102
本文介绍了如何管这里一个文档通过一个命令,并捕获结果到一个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在这个输出,我需要在标准输出值。我怎样才能捕捉到它变成一个变量,这样我可以在脚本的其余部分使用它?

要求:


  • 脚本必须在一个文件中。

  • 我preFER没有写任何临时文件,如果可能的话。

 #!/斌/庆典猫<< EOF | xsltproc的 -  ../pom.xml |尾-1
< XML版本=1.0&GT?;
<的xsl:样式版本=1.0的xmlns:XSL =htt​​p://www.w3.org/1999/XSL/Transform>
<的xsl:模板匹配=/><的xsl:value-of的选择=/项目/版/>< / XSL:模板>
< / XSL:样式>
EOF


解决方案

这似乎是工作(基于伊格纳西奥的答案)。通过使用一个子shell此处文档正确管道输送到xsltproc的同时仍然通过尾后通过。

  VERSION = $((xsltproc的 -  ../pom.xml |尾-1)LT;< EOF
< XML版本=1.0&GT?;
<的xsl:样式版本=1.0的xmlns:XSL =htt​​p://www.w3.org/1999/XSL/Transform>
<的xsl:模板匹配=/><的xsl:value-of的选择=/项目/版/>< / XSL:模板>
< / XSL:样式>
EOF

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

解决方案

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.

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
)

这篇关于如何管这里一个文档通过一个命令,并捕获结果到一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆