[ACCEPTED]-How to pipe a here-document through a command and capture the result into a variable?-heredoc
This seems to work (based on Ignacio's answer). By 3 using a subshell the here-document is correctly 2 piped into xsltproc while still being passed 1 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
)
The cat ... |
isn't necessary.
foo=$(sed 's/-/_/g' << EOF
1-2
3-4
EOF
)
0
I've been playing around with heredocs
for a week 31 or two. Here's an excerpt from my answer 30 to Is there a way to get actual (uninterpreted) shell arguments in a function or script? at Unix Stack Exchange that might help 29 illustrate their use a little for your case:
... EXCERPT: ...
Probably 28 you noticed the difference between the two 27 heredocs in the second example. The heredoc 26 EOF terminator within the function is unquoted, while 25 the one fed to read is quoted with single 24 quotes. In this way the shell is instructed 23 to perform expansion on the heredoc with 22 an unquoted terminator, but not to do so 21 when its terminator is quoted. It doesn't 20 break when expanding the unquoted heredoc 19 in the function because the value of the 18 variable it expands is already set as a 17 quoted string and it doesn't parse it twice.
Probably 16 what you want to do involves piping your 15 Windows path from the output of one command 14 into the input of another dynamically. Command 13 substitution within a heredoc makes this 12 possible:
% _stupid_mspath_fix() {
> sed -e 's@\\@/@g' -e 's@\(.\):\(.*\)@/drive/\1\2@' <<_EOF_
>> ${1}
>> _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 11 output the backslashes from some application 10 (I used printf above), then running that 9 command within $(...) and enclosing that 8 within an unquoted heredoc passed to another 7 application that can reliably accept the 6 backslashes as input (such as read and sed 5 above) will bypass the shell's parsing of 4 your backslashes altogether. Whether or 3 not the applications can handle the backslashes 2 as input/output is something you'll have 1 to find out for yourself.
-Mike
You don't need the tail -1
by adding omit-xml-declaration="yes"
:
VERSION=$(xsltproc - pom.xml << EOF
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mvn="http://maven.apache.org/POM/4.0.0">
<xsl:output method="text" omit-xml-declaration="yes"/>
<xsl:template match="/"><xsl:value-of select="mvn:project/mvn:version"/></xsl:template>
</xsl:stylesheet>
EOF
)
0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.