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 01-15-2016, 08:02 AM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,729

Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
Bash stdout redirection


I have code in a bash shell which writes to stdout as it runs.
This is a contrived example ...
Code:
echo
echo "Program <ProgName> started."
echo
echo "Entering process stage 2."
{Many lines of code}
echo "Interim results pass the sanity test."
echo
{Many lines of code}
echo
echo "Entering process stage 3."
{Many lines of code}
echo "Everything looks good so far."
echo
echo "The finished product is ..."
{Many lines of code}
echo
echo "Program <ProgName> ended."
exit
I'd like to redirect most of stdout to a Journal file.
How is this done?

Maybe something like this ...
Code:
echo
echo "Program <ProgName> started."
echo
From now on, until further notice,
   all stdout should be written to file ABC.
echo "Entering process stage 2."
{Many lines of code}
echo "Interim results pass the sanity test."
echo
{Many lines of code}
echo
echo "Entering process stage 3."
{Many lines of code}
echo "Everything looks good so far."
echo
From now on, until further notice,
   all stdout should be written to file XYZ.
echo "The finished product is ..."
{Many lines of code}
From now on, until further notice,
   all stdout should be written to the screen (as usual).
echo
echo "Program <ProgName> ended."
exit
Daniel B. Martin
 
Old 01-15-2016, 08:13 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,334

Rep: Reputation: 362Reputation: 362Reputation: 362Reputation: 362
One way to do it:

Code:
echo "This goes to stdout"
(
echo "But,"
echo "These go to a file"
) > /tmp/somefile
echo "And this to stdout again"
 
Old 01-15-2016, 08:33 AM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,729

Original Poster
Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
Quote:
Originally Posted by Guttorm View Post
Code:
echo "This goes to stdout"
(
echo "But,"
echo "These go to a file"
) > /tmp/somefile
echo "And this to stdout again"
This surely works but requires a whole lot of code changes. I'm hoping to find a way to insert those three From now on commands, if such things exist.

Daniel B. Martin
 
Old 01-15-2016, 08:52 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,334

Rep: Reputation: 362Reputation: 362Reputation: 362Reputation: 362
Here is something to try:

http://unix.stackexchange.com/questi...rest-of-script
 
2 members found this post helpful.
Old 01-15-2016, 09:31 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
I would use the exec option from the link above as well The nice part is you can put all the execs in the same piece of code and then use them when required.
On the other side of the fence, the original suggestion is actually a lot less typing and you could just use variables for the 3 file names.
 
1 members found this post helpful.
Old 01-15-2016, 09:40 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177
Also, commonly, "informational" messages are written to STDERR while "output" goes to STDOUT. By default both are directed to the terminal screen.
 
Old 01-15-2016, 02:09 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,729

Original Poster
Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
Quote:
Originally Posted by Guttorm View Post
Thank you, Guttorm. This worked nicely. SOLVED!

Daniel B. Martin
 
  


Reply

Tags
bash


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
STDOUT redirection problem in BASH qubuntu Linux - Newbie 6 04-05-2010 08:06 AM
How to redirect standard stdout to multi stdout ( Bash )? john.daker Programming 4 11-03-2008 11:20 PM
Automatically stdout redirection to a file tikit Linux - Newbie 4 10-10-2008 05:18 AM
redirection the process stdout do1do Linux - Security 3 11-23-2003 11:39 PM
redirection process stdout do1do Linux - General 2 11-23-2003 09:06 PM

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

All times are GMT -5. The time now is 10:52 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration