Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get exit status of piped command from inside the pipeline?
    primarykey
    data
    text
    <p>Consider I have following commandline: <code>do-things arg1 arg2 | progress-meter "Doing things...";</code>, where <code>progress-meter</code> is bash function I want to implement. It should print <code>Doing things...</code> before running <code>do-things arg1 arg2</code> or in parallel (so, it will be printed anyway at the very beginning), and record stdout+stderr of <code>do-things</code> command, and check it's exit status. If exit status is 0, it should print <code>[ OK ]</code>, otherwise it should print <code>[FAIL]</code> and dump recorded output.</p> <p>Currently I have things done using <code>progress-meter "Doing things..." "do-things arg1 arg2";</code>, and <code>eval</code>uating second argument inside, which is clumsy and I don't like that and believe there is better solution.</p> <p>The problem with pipe syntax is that I don't know how can I get <code>do-things</code>' exit status from <strong>inside</strong> the pipeline? <code>$PIPESTATUS</code> seems to be useful only after <strong>all</strong> commands in pipeline finished.</p> <p>Maybe process substitution like <code>progress-meter "Doing things..." &lt;(do-things arg1 arg2);</code> will be fine, but in this case I also don't know how can I get exit status of <code>do-things</code>.</p> <p>I'll be happy to hear if there is some other neat syntax possible to achieve same task without escaping command to be executed like in my example.</p> <p>I greatly hope for the help of community.</p> <p><i><strong>UPD1:</strong> As question seems not to be clear enough, I paraphrase it:</i></p> <p>I want bash function that can be fed with command, that will execute in parallel to function, and bash function will receive it's stdout+stderr, wait for completion and get its exit status.</p> <p>Example implementation using <code>eval</code>s:</p> <pre><code>progress_meter() { local output; local errcode; echo -n -e $1; output=$( { eval "${cmd}"; } 2&gt;&amp;1; ); errcode=$?; if (( errcode )); then { echo '[FAIL]'; echo "Output was: ${output}" } else { echo '[ OK ]'; }; fi; } </code></pre> <p>So this can be used as <code>progress_meter "Do things..." "do-things arg1 arg2"</code>. I want the same without eval.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload