LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-04-2010, 02:44 PM   #1
hmsdefender
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Rep: Reputation: 0
(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

#!/bin/sh
#
((hi.sh 3>1& 1>&2 2>&3) | tee /dev/tty) > err.log

Executing callit.sh I get the following output...

hi
./hi.sh: line 4: ehco: command not found
hi

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.

Any ideas? (be kind)

Ron
 
Old 03-04-2010, 06:14 PM   #2
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 4&5, Fedora 10, CentOS 5.4, IPCop
Posts: 569

Rep: Reputation: 55
Excellent question. Try this:-
Code:
hi.sh > >(tee -a all.log > /dev/null) 2> >(tee -a all.log err.log > /dev/null)
Have fun coming to grips with the command line !!!

Last edited by blacky_5251; 03-04-2010 at 06:16 PM.
 
Old 03-04-2010, 09:01 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,502

Rep: Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893
This worked for me:

Code:
 ./hi.sh 2>&1 1> all.log 2> err.log
My bad blacky ... I too have your output (should look at it all before I post next time :$

Last edited by grail; 03-05-2010 at 08:27 AM. Reason: dufus
 
Old 03-05-2010, 12:36 AM   #4
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 4&5, Fedora 10, CentOS 5.4, IPCop
Posts: 569

Rep: Reputation: 55
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.
 
Old 03-05-2010, 08:04 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
you can put it at the top of the script, (sh type shells only) thus:
Code:
#!/bin/sh
exec 2> err.log
exec >  out.log
...
...
...
 
Old 03-05-2010, 01:52 PM   #6
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 4&5, Fedora 10, CentOS 5.4, IPCop
Posts: 569

Rep: Reputation: 55
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script with auto installation and log errors Matafome Programming 4 01-20-2010 07:34 PM
cron job did not redirect output from bash script junust Programming 2 07-26-2009 04:30 AM
Bash script for server log (namely var/log/messages) tenaciousbob Programming 17 05-24-2007 10:43 AM
Redirect script output to log window in wxPython wapcaplet Programming 1 07-04-2004 08:59 PM
How to redirect standard output of piped command to log file andrewstr Linux - General 10 02-04-2004 02:07 PM


All times are GMT -5. The time now is 12:43 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration