[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: |
Andy Chu |
Subject: |
Re: [Help-bash] How to merge stdout and stderr yet distinguish what is from stdout and what is from stderr? |
Date: |
Sun, 4 Feb 2018 19:40:30 -0800 |
>
> I tried it multiple times. But the order is not guaranteed to be
> maintained, I guess it is due the buffer that might be used with awk
> when reading from stdout or stderr. So technically speaking, this
> solution does not solve my original question.
>
Well, if you want the same order line-by-line, I think you have to write a
program that redirects stdout and stderr to separate pipes, and does
select() over them.
Even then, I'm not sure there is any guarantee. If your program does this:
print 'stdout'
print >>sys.stderr, 'stderr'
Then when the select() wakes up, it should have data on both file
descriptors. And then it won't know which one to print first -- stdout or
stderr.
So it could be impossible not just in shell, **but in Unix**.
But if anyone has any other ideas I'm curious.
As I see it, there is a fundamental problem:
- A pipe/buffer in Unix is inherently ordered (in the kernel). If both
stdout and stderr are connected to the same pipe, lines will have a natural
order.
- If you want to tell stdout and stderr apart, then you need to write them
to separate pipes/buffers.
- Once they're in separate pipes/buffers, there is no longer a global
order. You lost that information.
The calling program would have to number its lines. But I don't think it
can be done without modifying the calling program.
Andy