Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After each command, the exit code can be found in the <code>$?</code> variable so you would have something like:</p> <pre><code>ls -al file.ext rc=$?; if [[ $rc != 0 ]]; then exit $rc; fi </code></pre> <p>You need to be careful of piped commands since the <code>$?</code> only gives you the return code of the last element in the pipe so, in the code:</p> <pre><code>ls -al file.ext | sed 's/^/xx: /" </code></pre> <p>will not return an error code if the file doesn't exist (since the <code>sed</code> part of the pipeline actually works, returning 0).</p> <p>The <code>bash</code> shell actually provides an array which can assist in that case, that being <code>PIPESTATUS</code>. This array has one element for each of the pipeline components, that you can access individually like <code>${PIPESTATUS[0]}</code>:</p> <pre><code>pax&gt; false | true ; echo ${PIPESTATUS[0]} 1 </code></pre> <p>Note that this is getting you the result of the <code>false</code> command, not the entire pipeline. You can also get the entire list to process as you see fit:</p> <pre><code>pax&gt; false | true | false; echo ${PIPESTATUS[*]} 1 0 1 </code></pre> <p>If you wanted to get the largest error code from a pipeline, you could use something like:</p> <pre><code>true | true | false | true | false rcs=${PIPESTATUS[*]}; rc=0; for i in ${rcs}; do rc=$(($i &gt; $rc ? $i : $rc)); done echo $rc </code></pre> <p>This goes through each of the <code>PIPESTATUS</code> elements in turn, storing it in <code>rc</code> if it was greater than the previous <code>rc</code> value.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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