[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] shell redirection
From: |
David Niklas |
Subject: |
Re: [Help-bash] shell redirection |
Date: |
Fri, 28 Oct 2016 09:42:22 -0400 |
On Wed, 19 Oct 2016 21:02:20 Russell Lewis wrote:
> On Wed, Oct 19, 2016 at 6:32 PM, John McKown
> <address@hidden> wrote:
>
> > On Wed, Oct 19, 2016 at 12:28 PM, Christof Warlich
> > <address@hidden> wrote:
> >
> > > Hi,
> > > looks like I still haven?t fully gasped shell redirection:
> > > I want my script(s) to print stderr to the terminal, while both
> > > stdout _/and/_ stderr should go to a logfile. I thought that this
> > > is an easy
> > task,
> > > but so far, I failed miserably.
> > > Any ideas how this could be done, ideally without using any
> > > temporary files or named pipes?
> > > Many thanks,
> > > Chris
> > >
> > >
> > ?I've read all the replies. And tried some really strange things
> > myself. The closest that I can come to what you want (and it is not
> > all that close) is to use the "script" program to run your program.
> >
> > script -c 'somecmd p1 parm2 --option' logfile.txt
> >
> > Both stdout & stderr come to the terminal & get written to
> > logfile.txt? . There is no way, that I know of, to put stdout into
> > "logfile.txt" but suppress it from the terminal.
> >
> >
> > --
> > Heisenberg may have been here.
> >
> > Unicode: http://xkcd.com/1726/
> >
> > Maranatha! <><
> > John McKown
> >
>
> As others have mentioned, there's no way to ensure that the lines reach
> the logfile in order (that problem exists even in the source program,
> but also in the tee command below). But here's a command line which
> seems to work (under trivial testing):
> { echo "fred"; echo "wilma" 1>&2; } 2> >(tee -a logfile) 1>>logfile
>
> Basically, it redirects stderr to a process substitution (that is, it
> connects stderr to the stdin of tee). The process substitution's
> stdout is of course the same as the current shell - so stderr goes both
> to the log file and to the tty; then we redirect stdout to point to the
> logfile as well.
>
> I used -a on the tee command, and >> on the redirection of stdout, so
> that neither would crop the log file (and presumably destroy the output
> of the other), but I'm not well versed in what happens when there are
> multiple open file pointers to the same file on disk. From trivial
> testing, it seems to work, but I worry that perhaps there are race
> conditions I haven't explored. (I originally thought about using some
> magic with /dev/fd to duplicate file pointers, but I wasn't sure that
> it was necessary complexity
> - or that I could get it totally correct.)
>
> Russ
>
Yuck, a top post (reformat).
I got a better idea.
stdbuf -oL -eL CMD 2> >(stdbuf -eL -oL tee -a logfile) 1>>logfile
This will work better (in theory if your lines are not multiline based).
Sincerely,
David
- Re: [Help-bash] shell redirection,
David Niklas <=