LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 02: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 08:22 AM.
 
Old 02-11-2010, 02:34 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
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 02:38 PM.
 
1 members found this post helpful.
Old 02-11-2010, 02:54 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

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, 04: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, 05: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 27 10-20-2017 03:45 AM
Bash Scripting: Redirect output of entire script to file & screen Kristijan Programming 3 10-12-2017 03:17 PM
[SOLVED] bash: how to redirect to file verbose output of mailx? val3xiv Linux - General 3 02-26-2009 07:02 PM
bash input/output redirect problem greno Programming 2 12-31-2006 03: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 03:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration