<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

      <title>Comments on: Pipe command output, but keep the error code</title>
      <link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code/</link>
      <description>Comments on Ask MetaFilter post Pipe command output, but keep the error code</description>
	  	  <pubDate>Sat, 24 Nov 2007 15:10:56 -0800</pubDate>
      <lastBuildDate>Sat, 24 Nov 2007 15:10:56 -0800</lastBuildDate>
      <language>en-us</language>
	  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
	  <ttl>60</ttl>

<item>
  	<title>Question: Pipe command output, but keep the error code</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code</link>	
  	<description>How do I get the correct return code from a unix command line application after I&apos;ve piped it through another command that succeeded? &lt;br /&gt;&lt;br /&gt; In detail, here&apos;s the situation writ small:&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;# mycmd_that_fails | postprocesor_that_succeeds&lt;br&gt;
# echo $?&lt;br&gt;
0&lt;/pre&gt;&lt;br&gt;
&lt;br&gt;
And, what I&apos;d like to see is:&lt;br&gt;
&lt;pre&gt;# mycmd_that_fails | postprocesor_that_succeeds&lt;br&gt;
# echo $?&lt;br&gt;
1&lt;/pre&gt;&lt;br&gt;
&lt;br&gt;
The obvious solution was to capture the output of mycmd_that_fails into a file and then pipe it through the postprocesor_that_succeeds, but since my command is a build script it&apos;s nice to see the progress as it runs.  I&apos;d rather have the postprocessor operate on the command as it runs than after the fact.&lt;br&gt;
&lt;br&gt;
Does anyone know how to accomplish this?</description>
  	<guid isPermaLink="false">post:ask.metafilter.com,2008:site.76984</guid>
  	<pubDate>Sat, 24 Nov 2007 14:55:15 -0800</pubDate>
  	<dc:creator>ChrisR</dc:creator>
	
	<category>linux</category>
	
	<category>unix</category>
	
	<category>administration</category>
	
	<category>pipe</category>
	
	<category>howto</category>
	
</item>
<item>
  	<title>By: freebird</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1143877</link>	
  	<description>You could maybe slip a little perl (or whatever) script between them to save off the return code for later use?&lt;br&gt;
&lt;br&gt;
mycmd | perl_thingy | postprocessor&lt;br&gt;
&lt;br&gt;
Where the perl_thingy could stick $? in a file, then postprocessor could read that file. More or less complicated, depending on what you&apos;re actually doing and your personal tastes, would be to have second perl script that would accept the postprocessor&apos;s output, read the stashed result from the first, and generate whatever kind of summary you like.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1143877</guid>
  	<pubDate>Sat, 24 Nov 2007 15:10:56 -0800</pubDate>
  	<dc:creator>freebird</dc:creator>
</item>
<item>
  	<title>By: Steven C. Den Beste</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1143887</link>	
  	<description>IIRC, this:&lt;br&gt;
&lt;br&gt;
mycmd_that_fails 2&amp;amp;&amp;gt; /path/erroroutputfile | postprocesor_that_succeeds&lt;br&gt;
&lt;br&gt;
Without that addition, all the outputs are merged and fed to the pipe. What that does is to separate out the error flow from the normal flow, and redirect the error flow to the file.&lt;br&gt;
&lt;br&gt;
But this only allows you to see the error after the fact.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1143887</guid>
  	<pubDate>Sat, 24 Nov 2007 15:31:57 -0800</pubDate>
  	<dc:creator>Steven C. Den Beste</dc:creator>
</item>
<item>
  	<title>By: Steven C. Den Beste</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1143888</link>	
  	<description>I think I botched that syntax. &amp;quot;two ampersand into&amp;quot; isn&apos;t right, but I&apos;m not sure what is.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1143888</guid>
  	<pubDate>Sat, 24 Nov 2007 15:32:56 -0800</pubDate>
  	<dc:creator>Steven C. Den Beste</dc:creator>
</item>
<item>
  	<title>By: Steven C. Den Beste</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1143889</link>	
  	<description>...what I shoulda said was...&lt;br&gt;
&lt;br&gt;
Also, that redirect can go to the console device instead of to a file if you use the proper &amp;quot;/dev/something&amp;quot; path.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1143889</guid>
  	<pubDate>Sat, 24 Nov 2007 15:34:04 -0800</pubDate>
  	<dc:creator>Steven C. Den Beste</dc:creator>
</item>
<item>
  	<title>By: flabdablet</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1143890</link>	
  	<description>You pretty much have to save the first command&apos;s result in a file, since each command in the pipeline is going to run inside its own environment.  So just have my_cmd_that_fails echo $? into a file at some appropriate point, and pick it up later.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1143890</guid>
  	<pubDate>Sat, 24 Nov 2007 15:34:32 -0800</pubDate>
  	<dc:creator>flabdablet</dc:creator>
</item>
<item>
  	<title>By: grouse</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1143892</link>	
  	<description>Use &lt;code&gt;${PIPESTATUS[*]}&lt;/code&gt; in bash. I have something like this in my .bashrc, which gives me the exit status of every command in the last pipe I just ran, if anything is not 0:&lt;br&gt;
&lt;br&gt;
&lt;code&gt;export&amp;nbsp;PROMPT_COMMAND=&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_RES=\${PIPESTATUS[*]};&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_RES_STR=&apos;&apos;;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;res&amp;nbsp;in&amp;nbsp;\$_RES;&amp;nbsp;do&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;[[&amp;nbsp;(&amp;nbsp;\$res&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;)&amp;nbsp;]];&amp;nbsp;then&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_RES_STR=\&amp;quot;&amp;nbsp;[\$_RES]\&amp;quot;;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fi;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;done&amp;quot;&lt;br&gt;&lt;br&gt;export&amp;nbsp;PS1=&amp;quot;\n\u@\h&amp;nbsp;\w\$_RES_STR\n\\$&amp;nbsp;&amp;quot;&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
Try it, you&apos;ll like it.&lt;br&gt;
&lt;br&gt;
The actual code puts things in color if you&apos;re using a terminal that supports that. But it&apos;s somewhat more complicated and therefore harder to understand. I can provide it if you want.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1143892</guid>
  	<pubDate>Sat, 24 Nov 2007 15:37:31 -0800</pubDate>
  	<dc:creator>grouse</dc:creator>
</item>
<item>
  	<title>By: puddpunk</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1143895</link>	
  	<description>Have you tried this:&lt;br&gt;
&lt;br&gt;
# my_cmd_that_fails; RET_VAL=$? | postprocessor_that_succeds; echo $RET_VAL&lt;br&gt;
&lt;br&gt;
You can then access $RETURN within that command. If you need to access it later use the export command:&lt;br&gt;
&lt;br&gt;
# my_cmd_that_fails; export RET_VAL=$? | postprocessor_that_succeeds&lt;br&gt;
# echo $RET_VAL</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1143895</guid>
  	<pubDate>Sat, 24 Nov 2007 15:40:00 -0800</pubDate>
  	<dc:creator>puddpunk</dc:creator>
</item>
<item>
  	<title>By: flabdablet</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1143896</link>	
  	<description>SCDB is apparently a little confused about the behaviour of standard output and standard error.  Only standard output file descriptor 1) is redirected down a pipeline by default.  Standard error (file descriptor 2) is &lt;strong&gt;not&lt;/strong&gt; merged with standard output.  That&apos;s kind of the point of standard error.&lt;br&gt;
&lt;br&gt;
If you &lt;strong&gt;want&lt;/strong&gt; them merged, you have to include the redirect 2&amp;gt;&amp;amp;1 on the command that generates them.&lt;br&gt;
&lt;br&gt;
In any case, this question is about the error return code ($?) rather than the error output stream.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1143896</guid>
  	<pubDate>Sat, 24 Nov 2007 15:40:08 -0800</pubDate>
  	<dc:creator>flabdablet</dc:creator>
</item>
<item>
  	<title>By: puddpunk</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1143897</link>	
  	<description>Typo, I mean &amp;quot;You can then access $RET_VAL within that command...&amp;quot;</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1143897</guid>
  	<pubDate>Sat, 24 Nov 2007 15:40:41 -0800</pubDate>
  	<dc:creator>puddpunk</dc:creator>
</item>
<item>
  	<title>By: flabdablet</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1143900</link>	
  	<description>puddpunk, I don&apos;t think that will work, since each command in the pipeline gets its own environment; RET_VAL won&apos;t be the same variable on both sides of the pipe.&lt;br&gt;
&lt;br&gt;
The PIPESTATUS array built into bash is a partial workaround for this, but in the general case, it&apos;s easier to save $? into a file at some appropriate point, and pick it up later.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1143900</guid>
  	<pubDate>Sat, 24 Nov 2007 15:44:34 -0800</pubDate>
  	<dc:creator>flabdablet</dc:creator>
</item>
<item>
  	<title>By: jepler</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1143901</link>	
  	<description>PIPESTATUS is the way to go, so long as you can use bash and not any other shell.  Here&apos;s a handy script that can filter the output of a program while preserving its exit value: &lt;blockquote&gt;&lt;pre&gt;#!/bin/bash&lt;br&gt;
PAT=$1&lt;br&gt;
shift&lt;br&gt;
&amp;quot;$@&amp;quot; 2&amp;gt;&amp;amp;1 | LANG=C egrep -ve &amp;quot;$PAT&amp;quot;&lt;br&gt;
exit ${PIPESTATUS[0]}&lt;br&gt;
&lt;/pre&gt;&lt;/blockquote&gt;Example usage:&lt;blockquote&gt;&lt;pre&gt;filterout &amp;quot;Uninitialized&amp;quot; gcc dodgy.c&lt;/pre&gt;&lt;/blockquote&gt;</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1143901</guid>
  	<pubDate>Sat, 24 Nov 2007 15:46:12 -0800</pubDate>
  	<dc:creator>jepler</dc:creator>
</item>
<item>
  	<title>By: jepler</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1143906</link>	
  	<description>It looks like there&apos;s a solution for ksh, but &lt;a href=&quot;http://www.unix.com/unix-dummies-questions-answers/13018-exit-status-command-pipe-line.html#post47559&quot;&gt; it&apos;s ugly&lt;/a&gt;.  &lt;a href=&quot;http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2005-06/1079.html&quot;&gt;more variations for various shells&lt;/a&gt;</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1143906</guid>
  	<pubDate>Sat, 24 Nov 2007 15:51:12 -0800</pubDate>
  	<dc:creator>jepler</dc:creator>
</item>
<item>
  	<title>By: mbrubeck</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1143908</link>	
  	<description>The &lt;a href=&quot;http://debaday.debian.net/2007/04/15/moreutils-a-collection-of-useful-command-line-tools/&quot;&gt;mispipe&lt;/a&gt; command from the &lt;a href=&quot;http://kitenet.net/~joey/code/moreutils/&quot;&gt;moreutils&lt;/a&gt; package will do this.  Just &apos;aptitude install moreutils&apos; on Debian or Ubuntu or similar.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1143908</guid>
  	<pubDate>Sat, 24 Nov 2007 15:56:43 -0800</pubDate>
  	<dc:creator>mbrubeck</dc:creator>
</item>
<item>
  	<title>By: foodgeek</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1143909</link>	
  	<description>In bash and others you can do:&lt;br&gt;
&lt;br&gt;
# mycmd &amp;amp;&amp;amp; postprocessor&lt;br&gt;
&lt;br&gt;
then you can just echo $? to see the output from the failed command since the post processor will only be run if the first command runs cleanly.  Obviously this is useless if you want the post processor to run even if your command fails...</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1143909</guid>
  	<pubDate>Sat, 24 Nov 2007 15:57:50 -0800</pubDate>
  	<dc:creator>foodgeek</dc:creator>
</item>
<item>
  	<title>By: mbrubeck</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1143922</link>	
  	<description>foodgeek, that doesn&apos;t pipe the output from mycmd into postprocessor.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1143922</guid>
  	<pubDate>Sat, 24 Nov 2007 16:30:11 -0800</pubDate>
  	<dc:creator>mbrubeck</dc:creator>
</item>
<item>
  	<title>By: rhizome</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1143946</link>	
  	<description>grouse and jepler have the way. &amp;quot;man bash&amp;quot; and search for PIPESTATUS (&amp;quot;/PIPESTATUS&amp;quot; while in the manpage, with slash and without quotes).</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1143946</guid>
  	<pubDate>Sat, 24 Nov 2007 17:28:35 -0800</pubDate>
  	<dc:creator>rhizome</dc:creator>
</item>
<item>
  	<title>By: foodgeek</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1143966</link>	
  	<description>mbrubeck, true &apos;nuf - but aside from it not working, it&apos;s a perfect solution! :)</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1143966</guid>
  	<pubDate>Sat, 24 Nov 2007 17:52:30 -0800</pubDate>
  	<dc:creator>foodgeek</dc:creator>
</item>
<item>
  	<title>By: philomathoholic</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1144060</link>	
  	<description>Here&apos;s a way to do it by making your own (named) pipe.&lt;br&gt;
&lt;code&gt;&lt;br&gt;
$ mkfifo pipe  #make a pipe, named &amp;quot;pipe&amp;quot;&lt;br&gt;
$ postprocesor_that_succeeds &amp;lt; pipe &amp;amp;  #the call to read from pipe will block until a program writes to pipe&lt;br&gt;
$ mycmd_that_fails &amp;gt; pipe&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
I hope the above is understandable, I included comments (which start at the # and extend to the end of the line, they are safe to put in the command line).&lt;br&gt;
&lt;br&gt;
To make sure this worked, I did this:&lt;br&gt;
&lt;code&gt;&lt;br&gt;
$ mkfifo pipe&lt;br&gt;
$ cat &amp;lt; pipe &amp;amp;&lt;br&gt;
$ bash  #start a new shell, just for the exit value&lt;br&gt;
$ exit 2 &amp;gt; pipe  #exit out of new shell&lt;br&gt;
$ echo $?  #back in first shell&lt;br&gt;
2&lt;br&gt;
$&lt;/code&gt;</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1144060</guid>
  	<pubDate>Sat, 24 Nov 2007 20:31:01 -0800</pubDate>
  	<dc:creator>philomathoholic</dc:creator>
</item>
<item>
  	<title>By: philomathoholic</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1144061</link>	
  	<description>Really though, just install moreutils and use mispipe (like mrbrubeck suggests). It&apos;s the non-hacked-together way to do it.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1144061</guid>
  	<pubDate>Sat, 24 Nov 2007 20:37:33 -0800</pubDate>
  	<dc:creator>philomathoholic</dc:creator>
</item>
<item>
  	<title>By: ChrisR</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1144326</link>	
  	<description>philomathoholic:&lt;br&gt;
&lt;br&gt;
That sounds promising, but I&apos;ll have to experiment to ensure that it works under older Solaris _and_ on cygwin.  That&apos;s a nice, tidy soultion, so I hope it works.&lt;br&gt;
&lt;br&gt;
puddpunk:  I tried yours as well, and it works well enough, so that might be the short-term solution.&lt;br&gt;
&lt;br&gt;
grouse:  You&apos;ve piqued my curiosity; The only question:  What are the indices in the PIPESTATUS array?  The order of commands in the last pipe?</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1144326</guid>
  	<pubDate>Sun, 25 Nov 2007 08:44:14 -0800</pubDate>
  	<dc:creator>ChrisR</dc:creator>
</item>
<item>
  	<title>By: grouse</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1144338</link>	
  	<description>&lt;em&gt;What are the indices in the PIPESTATUS array? The order of commands in the last pipe?&lt;/em&gt;&lt;br&gt;
&lt;br&gt;
Yes.&lt;br&gt;
&lt;br&gt;
Are you using bash? If so I think it is a no-brainer to use the built-in $PIPESTATUS array for what it was designed for, rather than hacky solutions or requiring the installation of another package.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1144338</guid>
  	<pubDate>Sun, 25 Nov 2007 09:02:56 -0800</pubDate>
  	<dc:creator>grouse</dc:creator>
</item>
<item>
  	<title>By: ChrisR</title>
  	<link>http://ask.metafilter.com/76984/Pipe-command-output-but-keep-the-error-code#1144391</link>	
  	<description>I&apos;ll have to check to see if I am able to do so on all of our build boxes; we&apos;ve got some older architectures that are... bash-unfriendly.&lt;br&gt;
&lt;br&gt;
I hope to be able to; I implemented it on the Linux architectures and it works like a charm.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.76984-1144391</guid>
  	<pubDate>Sun, 25 Nov 2007 10:29:15 -0800</pubDate>
  	<dc:creator>ChrisR</dc:creator>
</item>

    </channel>
</rss>
