Subject: bug#22195: deviation from POSIX in tee

From: Bernhard Voelker <[email-masked]>
Date: Fri, 18 Dec 2015 08:19:35 +0100

On 12/18/2015 04:22 AM, Paul Eggert wrote:
> Eric Renouf wrote:
>> If a write to any successfully opened file operand fails
> 
> But the write didn't fail here. Instead, a signal was sent to 'tee'. If you 
> don't want the signal, trap it. E.g.:
> 
> trap '' PIPE
> for i in {1..300}; do
>      echo "$i"
>      echo "$i" >&2
>      sleep 1
> done | tee >(head -1 > h.txt; echo "Head done") \
>   >(tail -1 > t.txt) >/dev/null
> 
> will give the behavior you want.
> 
> So there is no deviation from POSIX here.

Furthermore, tee got the new --output-error=MODE option in
version 8.24 to have more control over this:

  $ src/tee --help
  ...
    -p                        diagnose errors writing to non pipes
      --output-error[=MODE]   set behavior on write error.  See MODE below
  ...
  MODE determines behavior with write errors on the outputs:
    'warn'         diagnose errors writing to any output
    'warn-nopipe'  diagnose errors writing to any output not a pipe
    'exit'         exit on error writing to any output
    'exit-nopipe'  exit on error writing to any output not a pipe
  The default MODE for the -p option is 'warn-nopipe'.
  The default operation when --output-error is not specified, is to
  exit immediately on error writing to a pipe, and diagnose errors
  writing to non pipe outputs.

A minor note to your command: instead of redirecting tee's output to
/dev/null, you could use another pipe to save the write operations
in tee to it:

  producer \
    | tee --output-error=warn \
        >(head -1 > h.txt; echo "Head done") \
        | tail -1 > t.txt

Have a nice day,
Berny