LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-11-2010, 03:18 PM   #1
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Rep: Reputation: 17
Thumbs up [bash] redirect all subsequent std output to file


I have got a script with an outer and inner loop. The inner loop issues loads of echo's which need to be redirected to a log file determined by the outer loop.

The obvious solution is to redirect every echo to >$LOG and set LOG in the outer loop.

Code:
for f in $FILES ; do
    LOG=<logfile>
    for l in $LINES ; do
        ...
        echo blah >$LOG
        ...
        echo blah >$LOG
    done
done
I was just wondering whether it is possible to map stdout to $LOG in the outer loop without having to redirect every subsequent individual command output?

Last edited by hashbang#!; 11-08-2010 at 09:22 AM.
 
Old 02-11-2010, 03:34 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
If memory serves me you want to do something like this:

Code:
# redirect stdout to file descriptor
exec 6>&1
# redirect it to logfile
exec > /path/to/your/log/file.log

# your loop
(LOOP)

# fix your stdout and close file
exec 1>&6 6>&-
Actually, I found where my snippet came from as I was looking into this to make sure it was right (I lost a shell or two doing this with stdin.)

http://www.linuxtopia.org/online_boo...ripting_guide/

Check out chapter 16 "I/O Redirection", sub chapter 16.1 "Using exec". That has better details and more in-depth instruction than I can provide from memory.

Edit: I'd also suggest redirecting stderr or you might miss some messages from applications in your loop

Last edited by rweaver; 02-11-2010 at 03:38 PM.
 
1 members found this post helpful.
Old 02-11-2010, 03:54 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
Code:
for file in "${files[@]}" ; do
    log=<logfile>
    for line in "${lines[@]}" ; do
        ...
        echo blah
        ...
        echo blah
    done >> "$log"
done
 
1 members found this post helpful.
Old 02-11-2010, 05:20 PM   #4
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Original Poster
Rep: Reputation: 17
An answer to something I didn't think there was an answer to in under fifteen minutes - amazing!

How can you redirect stderr to the same file as stdout using the exec method?
 
Old 02-11-2010, 06:01 PM   #5
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Original Poster
Rep: Reputation: 17
Note:
Code:
exec 6>&1
exec >file.log
overwrites an existing file.log
Code:
exec 6>&1
exec >>file.log
redirects and appends to file.log
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH: How to Redirect Output to File, AND Still Have it on Screen edwin11 Linux - Software 25 07-02-2012 07:12 PM
[SOLVED] bash: how to redirect to file verbose output of mailx? val3xiv Linux - General 3 02-26-2009 08:02 PM
Bash Scripting: Redirect output of entire script to file & screen Kristijan Programming 1 09-11-2008 06:31 AM
bash input/output redirect problem greno Programming 2 12-31-2006 04:23 PM
Is there a way in Linux/Unix bash to turn off output or redirect to a file jimwelc Linux - Newbie 6 01-06-2005 04:56 AM


All times are GMT -5. The time now is 02:12 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration