Re: redirect the output to a file

From: Floyd L. Davidson (floyd_at_barrow.com)
Date: 08/10/04


Date: Mon, 09 Aug 2004 19:07:16 -0800

Edo <edwardoJE@aking.com> wrote:
>Hello
>I am trying to come up with this pipeing kind of cammnd inorder- to
>redirect the output of the "make clean; make" to a txt file.
>could some one help please
>thanks

That is perhaps more complex than it initially appears to be,
but if approached one step at a time it isn't difficult.

First, you have two commands executed in sequence. Then you
need to be concerned with both stdout and stderr, and last you
might want to see this on the screen as it all happens.

Lets go through that list in reverse order. Note that if any
of this doesn't make sense, you no doubt need to read the man
page for bash to see what each of these various operations does.

To both see what is going on and to save it to a file, use a
pipe to the /tee/ command.

   make | tee make.out

That will cause all output from /make/ to stdout to be sent to
/tee/, which will then write to both the screen and the
specified file, _make.out_.

However, error messages sent to stderr will only go to the screen.

   make 2>&1 | tee make.out

Will first redirect stderr to stdout, then send it all to /tee/.

The only problem left is to get two commands to combine in doing
that. You can run them in a subshell, and redirect the output
of that subshell:

   (make clean; make) 2>&1 | tee make.out

That command line will accomplish the whole smear...

There is a catch in this, which should be noted. If you *don't* want
it to show up on the screen, one might be inclined to try this:

   (make clean; make) 2>&1 > make.out

And discover that it doesn't work quite right! Stderr gets sent to
the terminal, not to the file. That is because the redirection for
stderr is done *before* stdout is redirected. To correct that,
change the order in which redirection is specified,

   (make clean; make) > make.out 2>&1

And then it works.

-- 
FloydL. Davidson           <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska)                         floyd@barrow.com


Relevant Pages