Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sending stderr to tty and logfile

55 views
Skip to first unread message

Lao Ming

unread,
Feb 18, 2009, 12:56:55 AM2/18/09
to
A function call is sending stdout to a file.
The script itself is sending stderr to a log.

Inside the function call (which is inside the script)
a printf needs to send stderr to both the tty and the log.

Would this be the way to do it?

printf "%s %s\n" "$RESULT" "$LOCATION" 2>&1 |tee "$log"

Barry Margolin

unread,
Feb 18, 2009, 1:05:16 AM2/18/09
to
In article
<fedc1c9a-81b4-4c78...@q30g2000prq.googlegroups.com>,
Lao Ming <laomi...@gmail.com> wrote:

This will send the message to the log and stdout, and you said stdout
goes to a file, so it will go to the log and the file, not the terminal.
Try this:

printf ... | tee "$log" >/dev/tty

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

Maxwell Lol

unread,
Feb 18, 2009, 6:49:26 AM2/18/09
to
Barry Margolin <bar...@alum.mit.edu> writes:

> This will send the message to the log and stdout, and you said stdout
> goes to a file, so it will go to the log and the file, not the terminal.
> Try this:
>
> printf ... | tee "$log" >/dev/tty

Or perhaps

printf ... | tee /dev/tty

which will send it to STDOUT.

Lao Ming

unread,
Feb 18, 2009, 1:16:31 PM2/18/09
to

So the command shouldn't contain the 2>&1 ?
I'm a little confused. What tells stderr
to go somewhere else ... in this case
to the logfile and the tty?

Janis Papanagnou

unread,
Feb 18, 2009, 2:24:00 PM2/18/09
to

And in the OP you asked:


>>>> Would this be the way to do it?
>>>>
>>>> printf "%s %s\n" "$RESULT" "$LOCATION" 2>&1 |tee "$log"

This would not send your data to the tty but to stdout (which might be
redirected in the outer scope and never hit the tty), so rather use an
additional redirection of stdout to /dev/tty as proposed upthread.

And yes, you need to redirect stderr as well - that was your original
question -, e.g. as you've done, by assigning stderr to the same channel
as stdout. But mind that this way you have no distinction of those two
channels (stdout and stderr) any more, which might be okay or not in
your context. If you want to keep the two channels separated in the
outer scope you have to do some severe (quite complex) i/o descriptor
manipulation.

Janis

Barry Margolin

unread,
Feb 18, 2009, 9:02:16 PM2/18/09
to
In article
<593521e6-ea6a-4b33...@p23g2000prp.googlegroups.com>,
Lao Ming <laomi...@gmail.com> wrote:

Early in the script you should do:

exec 2> "$log"

This tells stderr to go to the log file.

Then, when you want to write a message to stderr, you do:

printf ... >&2

and if you want it to go to both stderr and the terminal, write:

printf ... | tee /dev/tty >&2

William Pursell

unread,
Feb 21, 2009, 3:00:48 AM2/21/09
to
On 18 Feb, 05:56, Lao Ming <laoming...@gmail.com> wrote:

> printf "%s %s\n"   "$RESULT" "$LOCATION" 2>&1 |tee "$log"

What do you expect printf to write to its stderr here?

0 new messages