[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Merge stdout/stderr to a file yet preserve their distinc
From: |
Peng Yu |
Subject: |
Re: [Help-bash] Merge stdout/stderr to a file yet preserve their distinction as well |
Date: |
Thu, 15 Nov 2018 01:55:54 -0600 |
$ cat script.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:
echo stdout
echo stderr >&2
I figure out something like the following. But I am not sure whether
it is correct (especially whether the content in $tmpfile is in
order). Could you check what is the correct code?
tmpfile=$(mktemp -u)
exec 3>&1 4>&2
exec > >(tee -a "$tmpfile") 2> >(tee -a "$tmpfile" >&2)
./script.sh
exec 1>&3 2>&4
----
If I want to prepend something to stdout and stderr, I have the
following. Note that I can not use the commented exec (with the exec
removed), as it will switch the order of stdout and stderr. Why is it
so?
exec 3>&1 4>&2
exec 2> >(mawk -v eprefix='e: ' '{print eprefix $0 > "/dev/stderr";
fflush(); }') > >(mawk -v oprefix='o: ' '{print oprefix $0; fflush();
}')
#exec \
# > >(mawk -v oprefix='o: ' '{print oprefix $0; fflush(); }') \
# 2> >(mawk -v eprefix='e: ' '{print eprefix $0 > "/dev/stderr"; fflush(); }')
pid=$!
./script.sh
exec 1>&3 2>&4
wait "$pid"
On Wed, Nov 14, 2018 at 11:19 PM Dennis Williamson
<address@hidden> wrote:
>
>
>
> On Wed, Nov 14, 2018, 10:54 PM Peng Yu <address@hidden wrote:
>>
>> Hi,
>>
>> I'd like to pipe stdout/stderr to a file. But I also want them to be
>> printed to the terminal with them separately as stdout/stderr.
>>
>> It seems that none of the things mentioned below can satisfy both
>> requirements. Could anybody confirm whether it is indeed impossible to
>> satisfy both requirements? Thanks.
>>
>> https://stackoverflow.com/questions/363223/how-do-i-get-both-stdout-and-stderr-to-go-to-the-terminal-and-a-log-file
>> cmd 2>&1 | tee output.file
>>
>> --
>> Regards,
>> Peng
>>
>
>
> Look at
>
> https://mywiki.wooledge.org/BashFAQ/106
>
> under "complex duplicate logging".
--
Regards,
Peng