-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Is there a way to use a combination of redirection and other utilities (like tee) to execute a script where Output is shown on the screen *AND* The output to stdout and stderr is also directed to a log file? tia - ---Michael -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (GNU/Linux) iD8DBQFBTv3hjeziQOokQnARAqmCAJ0YyuuNqrdSqa/3SFy/BXsiYtOLSwCfY9Uu 6mNYtQq2M/mI+wgZhjll3+Y= =EmCa -----END PGP SIGNATURE-----
Today at 10:57am, Michael Satterwhite wrote:
Is there a way to use a combination of redirection and other utilities (like tee) to execute a script where
Output is shown on the screen
*AND*
The output to stdout and stderr is also directed to a log file?
This should do what you describe: your_application 2>&1 | tee logfile.log Jim
On Monday 20 September 2004 06:17 am, Jim Cunning wrote:
Today at 10:57am, Michael Satterwhite wrote:
Is there a way to use a combination of redirection and other utilities (like tee) to execute a script where
Output is shown on the screen
*AND*
The output to stdout and stderr is also directed to a log file?
This should do what you describe:
your_application 2>&1 | tee logfile.log
Jim
Jim, I know the > is for redirect but what the '2 &1' for? Thanks, Jerome
* Jerome Lyles
I know the > is for redirect but what the '2 &1' for?
google.com/linux search for 'bash redirection' -- Patrick Shanahan Registered Linux User #207535 http://wahoo.no-ip.org @ http://counter.li.org HOG # US1244711 Photo Album: http://wahoo.no-ip.org/photos
* Jerome Lyles
I know the > is for redirect but what the '2 &1' for?
http://www.tldp.org/LDP/abs/html/io-redirection.html -- Patrick Shanahan Registered Linux User #207535 http://wahoo.no-ip.org @ http://counter.li.org HOG # US1244711 Photo Album: http://wahoo.no-ip.org/photos
On 21-Sep-04 Jerome Lyles wrote:
On Monday 20 September 2004 06:17 am, Jim Cunning wrote:
Today at 10:57am, Michael Satterwhite wrote:
Is there a way to use a combination of redirection and other utilities (like tee) to execute a script where
Output is shown on the screen
*AND*
The output to stdout and stderr is also directed to a log file?
This should do what you describe:
your_application 2>&1 | tee logfile.log
Jim
Jim, I know the > is for redirect but what the '2>&1' for? Thanks, Jerome
Programs written in C and C++ by default have, as well as the
Standard Input stream "stdin", two output streams:
Standard Output "stdout" associated with the number "1"
Standard Error "stderr" associated with the number "2"
The program code will write normal output to "stdout" using
for instance the C function 'fprintf(stdout,"...",...)' and,
when some non-standard condition is detected, will write
warnings or error messages to "stderr" in a similar way.
Other messages (e.g. progress information) can also be written
to "stderr".
Usually, if you redirect Standard Output to a file e.g. as in
your_application > logfile.log
the contents of "stdout" go into logfile.log, while the contents
of "stderr" appear on the screen, and the two remain separate.
The effect of the redirection code "2>&1" is to cause the contents
of "stderr" (output stream "2") to be merged into the Standard Output
stream "stdout" (output stream "1") so that the above command would
fill logfile.log with both streams, as they occur, i.e. interspersed
and without distinction between one stream and the other (except in
so far as you can identify which is which by looking at it).
You could also collect "stderr" in a separate file by a call like
your_application > logfile.log 2>messages.txt
and then only the Standard Output would be in logfile.log, while
the Standard Error stream would go into the messages.txt file.
(This also illustrates that the "&1" acts like a filename
destination, designating the "stdout" stream as the destination
for "stderr".)
I hope this helps.
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding)
Jerome wrote regarding 'Re: [SLE] Redirection puzzle' on Tue, Sep 21 at 17:43:
On Monday 20 September 2004 06:17 am, Jim Cunning wrote:
Today at 10:57am, Michael Satterwhite wrote:
Is there a way to use a combination of redirection and other utilities (like tee) to execute a script where
Output is shown on the screen
*AND*
The output to stdout and stderr is also directed to a log file?
This should do what you describe:
your_application 2>&1 | tee logfile.log
Jim
Jim, I know the > is for redirect but what the '2 &1' for?
It reopens (dup2()'s) filehandle 1 as filehandle 2. 2 is STDERR, 1 is STDOUT by default. man dup2 may help. BTW, you can't easily have STDOUT go to the screen while both STDERR and STDOUT go to a file. The command above will send both to the screen and both to a file. It'd be relatively easy to write a program to do so, by moving file handles around and then exec()ing the desired application - but we'll just leave that as an excercise for the reader. :) --Danny
On Wednesday 22 September 2004 08:05, Danny Sauer wrote:
Jerome wrote regarding 'Re: [SLE] Redirection puzzle' on Tue, Sep 21 at 17:43:
On Monday 20 September 2004 06:17 am, Jim Cunning wrote:
Today at 10:57am, Michael Satterwhite wrote:
Is there a way to use a combination of redirection and other utilities (like tee) to execute a script where
Output is shown on the screen
*AND*
The output to stdout and stderr is also directed to a log file?
This should do what you describe:
your_application 2>&1 | tee logfile.log
Jim
Jim, I know the > is for redirect but what the '2 &1' for?
It reopens (dup2()'s) filehandle 1 as filehandle 2. 2 is STDERR, 1 is STDOUT by default. man dup2 may help.
BTW, you can't easily have STDOUT go to the screen while both STDERR and STDOUT go to a file. The command above will send both to the screen and both to a file. It'd be relatively easy to write a program to do so, by moving file handles around and then exec()ing the desired application - but we'll just leave that as an excercise for the reader. :)
Not true! Assume "chatty" is a program that writes "output" to the standard output stream and "error" to the standard error stream and that it writes the standard output first. The putative "chatty" program can be created as a trivial script: -==--==--==--==--==--==--==--==--==- #!/bin/bash --norc echo output echo error >&2 -==--==--==--==--==--==--==--==--==- % chatty output error % chatty >file error % cat file output % chatty 2>file output % cat file error % chatty >ofile 2>efile % cat ofile output % cat efile error # Here's the one Danny says "can't easily" be done: % (chatty |tee /dev/tty) >both 2>&1 ouput % cat both output error
--Danny
Randall Schulz
Randall wrote regarding 'Re: [SLE] Redirection puzzle' on Wed, Sep 22 at 13:12: [...]
# Here's the one Danny says "can't easily" be done: % (chatty |tee /dev/tty) >both 2>&1 ouput
% cat both output error
Are you sure about that? For some reason, I'm only seeing one line in the output file, either stdout or stderr. It's like there's some race condition with the file creation or something. Your solution *should* work, AFAIK, but doesn't work on my system... I put it inside of a loop and ran it a bunch of times, each time getting just one line in the "file" file, though that line was sometimes "stderr" and sometimes "stdout"... dsauer@danny-pc:/tmp> cat ./test #!/bin/bash echo stdout echo stderr > /dev/stderr dsauer@danny-pc:/tmp> (./test | tee /dev/tty) >file 2>&1 stdout dsauer@danny-pc:/tmp> cat file stderr dsauer@danny-pc:/tmp> Either way, I'm pretty sure the newbies on the list would disagree that said solution was "easy" to come up with. :) --Danny
Danny, On Thursday 23 September 2004 12:44, Danny Sauer wrote:
Randall wrote regarding 'Re: [SLE] Redirection puzzle' on Wed, Sep 22 at 13:12: [...]
# Here's the one Danny says "can't easily" be done: % (chatty |tee /dev/tty) >both 2>&1 ouput
% cat both output error
Are you sure about that? For some reason, I'm only seeing one line in the output file, either stdout or stderr. It's like there's some race condition with the file creation or something. Your solution *should* work, AFAIK, but doesn't work on my system... I put it inside of a loop and ran it a bunch of times, each time getting just one line in the "file" file, though that line was sometimes "stderr" and sometimes "stdout"...
I did test it before sending, and it did what I expected and intended. Offhand, I cannot think of a race condition created by this command. What shell do you use for interactive shell sessions? Did you try the "--norc" option in the script? I use it as a matter of course, both to speed script start-up and to insulate scripts from the vagaries of invoker's shell configuration and customization. I tried the tcsh counterpart: bash> tcsh tcsh> (chatty |tee /dev/tty) >& both output tcsh> cat both error output tcsh> exit exit bash> and that worked there, too. I'm not sure about the more subtle redirection details and syntaxes in shells other than BASH. (And even there, I learned something new just now scanning the manual for clues as to why this might not be working for you--did you know you can access TCP and UDP ports via special BASH-interpreted "/dev/tcp/port" and "/dev/udp/port" redirections?)
dsauer@danny-pc:/tmp> cat ./test #!/bin/bash echo stdout echo stderr > /dev/stderr dsauer@danny-pc:/tmp> (./test | tee /dev/tty) >file 2>&1 stdout dsauer@danny-pc:/tmp> cat file stderr dsauer@danny-pc:/tmp>
Either way, I'm pretty sure the newbies on the list would disagree that said solution was "easy" to come up with. :)
--Danny
Randall Schulz
Randall wrote regarding 'Re: [SLE] Redirection puzzle' on Thu, Sep 23 at 16:32:
Danny,
On Thursday 23 September 2004 12:44, Danny Sauer wrote:
Randall wrote regarding 'Re: [SLE] Redirection puzzle' on Wed, Sep 22 at 13:12: [...]
# Here's the one Danny says "can't easily" be done: % (chatty |tee /dev/tty) >both 2>&1 ouput
% cat both output error
Are you sure about that? For some reason, I'm only seeing one line in the output file, either stdout or stderr. It's like there's some race condition with the file creation or something. Your solution *should* work, AFAIK, but doesn't work on my system... I put it inside of a loop and ran it a bunch of times, each time getting just one line in the "file" file, though that line was sometimes "stderr" and sometimes "stdout"...
I did test it before sending, and it did what I expected and intended.
Offhand, I cannot think of a race condition created by this command.
What shell do you use for interactive shell sessions? Did you try the "--norc" option in the script? I use it as a matter of course, both to speed script start-up and to insulate scripts from the vagaries of invoker's shell configuration and customization.
I'm running regular bash as a shell. dsauer@danny-pc:/tmp/blah> /bin/bash --version GNU bash, version 2.05b.0(1)-release (i586-suse-linux) Copyright (C) 2002 Free Software Foundation, Inc. It's weird. The computer restarted today (crummy whole building UPS is appearently not working, so it seems) and I get the same behaviour. If I take tee out of the loop, so I just run "test >file 2>&1" it does the same thing. That's just wrong. the only odd thing about this machine is that it's SMP, but that's not something that should affect this. I tested it on a uniprocessor SuSE 9.1 install with the same bash version, and it does the same thing. Here's something odd off of another old SuSE machine: dsauer@newwww:/tmp > echo > /dev/stderr bash: /dev/stderr: Permission denied Maybe this building isn't a good place for bash redirection... Anyway, changing /dev/stderr to >&2 on that machine gets around the problem, and it works fine. That's 2.04.0(1) on SuSE 7.1. I've gotta rebuild my kernel so I can fire vmware back up so I can test on a 9.0 install, and maybe on an out-of-the-box 9.1. I'm presently running the 2.05b-305.1 bash package, which was an update from the 2.05b-305 package that ships with 9.1. Mmmm, lots of version info. [...]
I'm not sure about the more subtle redirection details and syntaxes in shells other than BASH. (And even there, I learned something new just now scanning the manual for clues as to why this might not be working for you--did you know you can access TCP and UDP ports via special BASH-interpreted "/dev/tcp/port" and "/dev/udp/port" redirections?)
You know, I stumbled across that in the man page yesterday, too - while I was looking for possible causes. Here I've been piping things through netcat for years when I could just redirect to /dev/tcp/host/port. I spent the next few minutes trying to write a web download client in bash. :) All those people who don't read man pages are really missing out. :) --Danny, who also enjoys some of the comments in the kernel config "help" sections
On Wednesday 22 September 2004 05:05 am, Danny Sauer wrote:
Jerome wrote regarding 'Re: [SLE] Redirection puzzle' on Tue, Sep 21 at 17:43:
On Monday 20 September 2004 06:17 am, Jim Cunning wrote:
Today at 10:57am, Michael Satterwhite wrote:
Is there a way to use a combination of redirection and other utilities (like tee) to execute a script where
Output is shown on the screen
*AND*
The output to stdout and stderr is also directed to a log file?
This should do what you describe:
your_application 2>&1 | tee logfile.log
Jim
Jim, I know the > is for redirect but what the '2 &1' for?
It reopens (dup2()'s) filehandle 1 as filehandle 2. 2 is STDERR, 1 is STDOUT by default. man dup2 may help.
BTW, you can't easily have STDOUT go to the screen while both STDERR and STDOUT go to a file. The command above will send both to the screen and both to a file. It'd be relatively easy to write a program to do so, by moving file handles around and then exec()ing the desired application - but we'll just leave that as an excercise for the reader. :)
--Danny
Sounds like a plan:-) At least I have all the pieces to the puzzle. Thanks, Jerome
participants (7)
-
Danny Sauer
-
Jerome Lyles
-
Jim Cunning
-
Michael Satterwhite
-
Patrick Shanahan
-
Randall R Schulz
-
Ted.Harding@nessie.mcc.ac.uk