Solved

Bash Script Output

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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