Solved

Bash Script Output

Posted on 2011-03-04
4
408 Views
Last Modified: 2012-06-27
I have a bash script that I am working on, each line can have output and error. I am trying to send all output to a file and error to a different file.

My goal is to have a report at the end that I can make use of and in case of problems I would view my error report.

Now the way I am doing this is like this

R=/path/to/reportfile
E=/path/to/errorrepot

And in every line I do this

some syntax > $R 2> $E

I find this too much consuming and not professional. Is there a way to generalize this. I mean is there a way to tell the script to send any output to $R and any error to $E at the begining of the script or do I have to do this in each line.

I am aware that while running the script I can do a > somefile and 2> someotherfile.

I would like to have this inside the script.

Thanks
0
Comment
Question by:small_student
  • 2
  • 2
4 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 35036050
Here you go:

exec 1>/path/to/reportfile
exec 2>/path/to/errorreport

Place the above at the top of your script, just after the shebang line, if any (#!/bin/sh).

wmp

0
 
LVL 14

Author Comment

by:small_student
ID: 35036191
Hi woolmilkporc,

Thanks for the quick responce but can you please explain these to me. I understand that 1 represents STDOUT and 2 represents STDERR.

Does this mean that you are changing the STDOUT and STDERR streams to a file thus redirecting everthing to them.

0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35036223
Yes, correct.

"exec" is used here for global redirecting of FD 1 (STDOUT) and FD 2 (STDERR) to the respective files.

wmp

 
0
 
LVL 14

Author Closing Comment

by:small_student
ID: 35036406
Thank a lot.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now