help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] How to merge stdout and stderr yet distinguish what is f


From: Tadeus Prastowo
Subject: Re: [Help-bash] How to merge stdout and stderr yet distinguish what is from stdout and what is from stderr?
Date: Mon, 5 Feb 2018 15:31:12 +0100

On Mon, Feb 5, 2018 at 2:54 PM, Greg Wooledge <address@hidden> wrote:
> On Sun, Feb 04, 2018 at 06:03:37PM -0600, Peng Yu wrote:
>> I tried it multiple times. But the order is not guaranteed to be
>> maintained,
>
> Correct!  THAT is the real issue here.

Won't `stdbuf' help?

Assuming that cmd is single threaded, `stdbuf -o0 -e0 cmd' should wake
up either the stdout or stderr reader process after writing a single
character to either stdout or stderr.  So, if the reader processes are
also run with stdbuf -i0 -oL, it increases the chance for the cmd and
its reader processes to run in a lock step.  And since the cmd is
single threaded, the relative ordering of the entries in the merged
stdout-stderr pipe should be almost correct.

BTW, I am having the following in mind when I talk about "merged
stdout-stderr pipe":
exec {mystdout}>&1
coproc pA (sed 's%^%[From stdout] %' >&${mystdout})
coproc pB (sed 's%^%[From stderr] %' >&${mystdout})
coproc (echo 'Forget pB as pA has already been forgotten' >/dev/null)
cmd 1>&${pA[1]} 2>&${pB[1]}; exec {pA[1]}>&- {pB[1]}>&- {mystdout}>&-

A sample cmd that I have in mind is attached, and its expected output
with regard to the correct relative ordering in the merged
stdout-stderr pipe is as follows:
[From stdout] 1
[From stdout] 2
[From stdout] 3
[From stdout] 4
[From stderr] 5
[From stderr] 6
[From stderr] 7
[From stderr] 8
[From stdout] 9
[From stderr] 10
[From stdout] 11
[From stderr] 12
[From stdout] 13
[From stderr] 14
[From stdout] 15
[From stderr] 16
[From stdout] 17
[From stdout] 18
[From stdout] 19
[From stderr] 20
[From stderr] 21
[From stdout] 22

--
Best regards,
Tadeus

Attachment: x.sh
Description: Bourne shell script


reply via email to

[Prev in Thread] Current Thread [Next in Thread]