Solved

Bash Script Output

Posted on 2011-03-04
4
413 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

911 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

24 Experts available now in Live!

Get 1:1 Help Now