Solved

Bash Script Output

Posted on 2011-03-04
4
420 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:Monis Monther
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Monis Monther
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:Monis Monther
ID: 35036406
Thank a lot.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
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…
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…

726 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