Knowledgebase

BASH - STDOUT question

Posted by mindbend, 07-02-2009, 03:35 PM
I'm not sure if this is possible or not, but I was hoping someone might know. I want to set an env variable inside of a script which directs all output into one file or another without piping the data. Is this possible? It is easy to just ./script > file, but what if I want output to go into multiple files? I can use >> file and >> file2 throughout the script but curiosity has me thinking there might be a way to set it at once place for all further output, then reset it again. Any ideas?

Posted by dexxtreme, 07-02-2009, 05:02 PM
You can try piping the output of the script through "tee". You can push the same output to as many files as you want, and STDOUT still comes through the screen (if you want to pipe the results to a final file). E.g., "./script | tee file1 file2 > file3" should end up generating file1, file2, and file3 with the exact same contents (STDOUT from "./script").

Posted by mindbend, 07-02-2009, 05:13 PM
Well, that takes it from outside in. I want to go inside out. It is a hypothetical problem which may not have a solution how I'm envisioning it.

Posted by RemyHorton, 07-02-2009, 07:07 PM
Taking guesses at what you mean but.. Could setup a named pipe, which in turn is piped to the file. Bit of a hack though..

Posted by foobic, 07-02-2009, 07:18 PM
To redirect STDOUT from within a script use: (STDOUT is 1, STDERR is 2)

Posted by mindbend, 07-03-2009, 04:29 PM
Eh I don't think its possible to do what I was wanting to do and it is just as easy to redirect from outside in. Thank you for the suggestions though! Long story short I had a script which collects data from about 40 different places and I wanted to conditionally output the data into separate files without forcing every line with >> $userActFile or whatever. I spent the time to add the conditions since being lazy won't fix the issue. Thank you though!



Was this answer helpful?

Add to Favourites Add to Favourites

Print this Article Print this Article

Also Read
Security Cleanup (Views: 575)
Citadelhost Awesome (Views: 613)
JustHost or HostGator (Views: 606)


Language:

Client Login

Email

Password

Remember Me

Search