title: How to implement 'set -o pipefail' in a POSIX way - almost done, expert help needed
body: <p>I have to implement the BASH <code>set -o pipefail</code> option in a POSIX way so that it works on various LINUX/UNIX flavors. To explain a bit, this option enables the user to verify the successful execution of all piped commands. With this option enabled this command <code>cat app.log | grep 'ERROR'</code> fails if <code>cat</code> fails, otherwise the <code>cat</code> error is suppressed. </p>
  
  <p>So, I found a really nice solution here: <a href="http://cfaj.ca/shell/cus-faq-2.html" rel="noreferrer">http://cfaj.ca/shell/cus-faq-2.html</a></p>
  
  <pre><code>      run() {
           j=1
           while eval "\${pipestatus_$j+:} false"; do
             unset pipestatus_$j
             j=$(($j+1))
           done
           j=1 com= k=1 l=
           for a; do
             if [ "x$a" = 'x|' ]; then
               com="$com { $l "'3&gt;&amp;-
                           echo "pipestatus_'$j'=$?" &gt;&amp;3
                         } 4&gt;&amp;- |'
               j=$(($j+1)) l=
             else
               l="$l \"\$$k\""
             fi
             k=$(($k+1))
           done
           com="$com $l"' 3&gt;&amp;- &gt;&amp;4 4&gt;&amp;-
                      echo "pipestatus_'$j'=$?"'
           exec 4&gt;&amp;1
           eval "$(exec 3&gt;&amp;1; eval "$com")"
           exec 4&gt;&amp;-
           j=1
           while eval "\${pipestatus_$j+:} false"; do
             eval "[ \$pipestatus_$j -eq 0 ]" || return 1
             j=$(($j+1))
           done
           return 0
         }
  </code></pre>
  
  <p>The above-mentioned <code>run()</code> function enables the user to invoke the piped commands in such a way:</p>
  
  <pre><code>run cmd1 \| cmd2 \| cmd3
  </code></pre>
  
  <p>If one of the commands fails you get it in <code>$?</code></p>
  
  <p>There is a problem however, it does not support the grouping of commands between pipes. I want to be able to invoke something like this:</p>
  
  <pre><code>run echo "test" ; grep "test" \| awk '{print}'
  </code></pre>
  
  <p>When I do it, the invocation fails. I cannot get the right modification to support the grouping of commands -- the script is a bit too complex for my bash skills... </p>
  
  <p>Could somebody help?</p>
  
  <p>Thanks!</p>