(Bash) Redirect all output from script to all.log and copy of errors to err.log
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
(Bash) Redirect all output from script to all.log and copy of errors to err.log
For simplicity assume the following script "hi.sh"
#!/bin/sh
#
echo hi
ehco hi
echo hi
Executing hi.sh produces the following on the screen ...
hi
./hi.sh: line 4: ehco: command not found
hi
I want to save the combined output (stdout & stderr) to a file called all.log, but also save stderr to a file called err.log. I've tried several methods using snippets regarding redirecting i/o from numerous forums and texts, but have been unsuccessful.
I tried calling hi.sh from another script (this is more like what I am doing for real) to see what results I get. Assuming the following script called callit.sh
However, if I use "callit.sh > all.log" I get the following output...
hi
hi
./hi.sh: line 4: ehco: command not found
In both cases err.log contains ...
./hi.sh: line 4: ehco: command not found
The file all.log is empty. I've tried many variations of the call within callit.sh and using redirect outside of callit.sh. The best I can get is err.log with...
./hi.sh: line 4: ehco: command not found
and all.log with ...
hi
hi
./hi.sh: line 4: ehco: command not found
As you can see in the later, the error message is out of sequence with the rest of the output due to subshell issues. Not an problem with a small example, but is the contents of all.log is useless if there are thousands of lines of content, but the errors are bunch at the end.
I don't see how it could. Can you post the log files? When I do that, this is what I get:-
Code:
[irb@eddie bin]$ ./hi.sh 2>&1 1> all.log 2> err.log
[irb@eddie bin]$ more *.log
::::::::::::::
all.log
::::::::::::::
hi
hi
::::::::::::::
err.log
::::::::::::::
./hi.sh: line 4: ehco: command not found
[irb@eddie bin]$
The first redirection (2>&1) redirects stderr to stdout, which at that point in the process is still going to the screen. Then you redirect stdout to all.log, which means the non-error statements (2 x echo) send their output to all.log, but it has no impact on the first redirection you made (i.e. stderr is still going to the screen). Then stderr is redirected (again) to err.log. So your command can be rewritten as:-
Code:
./hi.sh 1> all.log 2> err.log
If this is in fact working for you (which it shouldn't be), which distro and shell are you using?
Last edited by blacky_5251; 03-05-2010 at 12:37 AM.
Does that get stderr into the same log file (all.log) as stdout though? I would have thought that just puts stderr in one file and stdout in another.
The OP's need to have stderr and stdout combined contemporaneously in one file, plus stderr in another file, means this is something that "standard" redirection can't do.
Last edited by blacky_5251; 03-06-2010 at 12:18 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.