|
From: | Marco Ippolito |
Subject: | Re: Is there a way to make sure 141 exit status is resulted when pipeline is terminated prematurelly? |
Date: | Wed, 7 Oct 2020 02:11:34 +0100 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 |
yOn 06/10/2020 23:20, Peng Yu wrote:
Hi, The following examples show that 141 is not always resulted when a pipeline is terminated prematurelly. But I'd like to make sure any script which calls a pipeline can determine whether the pipeline is terminated prematurally. Is there a way to obtain such info in the script? Thanks. $ seq 10 | head -n 3; declare -p PIPESTATUS 1 2 3 declare -a PIPESTATUS=([0]="0" [1]="0") $ seq 10000000 | head -n 3; declare -p PIPESTATUS 1 2 3 declare -a PIPESTATUS=([0]="141" [1]="0")
Buffers.In your first example, seq is not being terminated by SIGPIPE whereas in your second example it is, so the PIPESTATUS is correct.
To verify this is the case, on Linux I prefix your commands with: strace -f -e 'trace=!all' ...If you wanted to force the signal arriving before program exit, you could introduce a delay, e.g.:
strace -f -e 'trace=!all' bash -c 'echo $$; while ((i++ < 4)); do echo $i; sleep .5; done' | head -n 3; declare -p PIPESTATUS
head would exit 0 normally after 3 lines and terminate the counting program via SIGPIPE.
Marco Ippolito maroloccio@gmail.com
[Prev in Thread] | Current Thread | [Next in Thread] |