I have long pipeline of commands in zsh
script:
pv /dev/sda > sda.raw | sha256sum > sda.raw.sha256 | cut -c61-64 | read SHASUM
how can I check inside an if
statement, that all commands exited successfully?
I know about ${pipestatus[@]}
, but it is an array. So I would have to check each element individually.
Is there some shortcut to check that ${pipestatus[@]}
contains only 0
?
UPDATE:
I have discovered some weird behavior:
for testing, I set up small filesystem (10MB) so that it fills up quickly:
#!/bin/zsh
set -o pipefail
if pv /dev/zero > loop/file ; then
echo OK
else
echo FAIL
fi
as expected, I get following error, and "FAIL" printed:
pv: write failed: No space left on device
However, when I change the pv command just slightly, it suddenly does not print any error, and the pv command seems to run indefinitely (even though there is no space left on the device)
#!/bin/zsh
set -o pipefail
if pv /dev/zero > loop/file | sha256sum ; then
echo OK
else
echo FAIL
fi
pipefail
and check$?
?pv
seems to write to a file via a redirection, meaningsha256sum
won't ever get anything to process. Likewise, the output ofsha256sum
is redirected to a file, so even if it produced output,cut
would never get anything to work with. Consequently, the finalread
won't do anything as all the data is written to files instead of to the next stage of the pipeline. If the redirections are removed, theread
might read something into theSHASUM
variable, but this is not what our question is about.pv
doesn't fail there, so of course there's no failure to detect and so it's a completely different problem than the original question).