[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: pipes that write to a file
From: |
Philip Rowlands |
Subject: |
Re: pipes that write to a file |
Date: |
Sun, 27 Jun 2021 23:35:24 +0100 |
User-agent: |
Cyrus-JMAP/3.5.0-alpha0-530-gd0c265785f-fm-20210616.002-gd0c26578 |
On Sun, 27 Jun 2021, at 01:13, Bruno Haible wrote:
> So, people come to recommend the 'sponge' program from moreutils [2]:
>
> prog1 < FILE | prog2 | prog3 | sponge FILE
>
> But this program has the drawback that, requiring another step in the
> pipeline, the exit code of the previous command gets lost.
To this specific point, there are bash options (not sure about other shells)
Option 1:
bash provides PIPESTATUS as an array of exit codes.
$ seq 50 | grep apple | wc -l
0
$ echo ${PIPESTATUS[*]}
0 1 0
Option 2:
bash provides the "pipefail" to expose a non-zero exit status from mid-pipeline.
$ set -o pipefail
$ seq 50 | grep apple | wc -l
0
$ echo $?
1
Cheers,
Phil