?
Solved

Bash Script Output

Posted on 2011-03-04
4
Medium Priority
?
423 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 2000 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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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…
Suggested Courses

777 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