Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Mixing and multiple redirection of stderr/stdout

On May 18, 11:18am, Michael Lehn wrote:
} Am 18.05.2011 um 07:13 schrieb Aaron Davies:
} > Relatedly, can these (or any other) redirections guarantee the
} > same interleaving of stdout and stderr that would be produced on
} > the console with no redirections? I've often seen (mostly in other
} > shells, iirc) that a process which would have, e.g., alternating
} > prints to out and err, is captured to file as one long block of out
} > followed by one long block of err.
} So can something like my "mixing stdout&stderr but also redirect
} stdout, stderr separately to files" work at all? The longer I think
} about it the more I am confused how it could be realized technically.
} I guess it can not be done by using "open, close, dup and co"
} internally. Or am I wrong?

In fact under the covers zsh is arranging something similar to

    exec 3>file_mix
    { script.sh |
      tee -a /proc/fd/3 |
      cat > file_out } 2>&1 |
     tee -a /proc/fd/3 |
     cat > file.err

so there is pipe buffering introduced that can affect the order of the
output in the mixed file even though the file is opened only once.

Even with >file_mix 2>&1 though, the order of output in the file can
be different from the order of output to the terminal because the
default OS buffering strategy for file descriptors may differ based
on the kind of device being written.  The introduction of extra pipes
just exaggerates this effect.

On the other hand zsh's internal tee/cat analogs are optimized for
efficiency rather than for preservation of ordering.  They could be
redone using select() and non-blocking descriptors to create a much
closer approximation of the interleaved write.

Messages sorted by: Reverse Date, Date, Thread, Author