LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-14-2008, 01:22 PM   #1
adelie
LQ Newbie
 
Registered: May 2008
Posts: 3

Rep: Reputation: 0
How to access a redirected output file from within a script


Hi,

I would like to access the redirected output (that is usually written to some file) from within the script. This script is run by many folks so I don't have control on how they name the output file. Is there a way to figure out the name from within the script ?

Thanks
\naren
 
Old 05-14-2008, 01:28 PM   #2
Eupator
LQ Newbie
 
Registered: May 2008
Distribution: Slackware/Slackintosh
Posts: 28

Rep: Reputation: 0
Can you be more specific? What script? Can you post its contents?
 
Old 05-14-2008, 03:49 PM   #3
adelie
LQ Newbie
 
Registered: May 2008
Posts: 3

Original Poster
Rep: Reputation: 0
I have a shell script: myscript.sh >& myscript.log

Inside this script I would like to be able to monitor myscript.log for some diagnostic messages. The problem is that I don't know in the general case where the output is being redirected to. Since many users could potentially use this script, they may choose to either 1) not redirect output 2) use their own filename to redirect the output.

I would like to know if the script from within can monitor its output log regardless of where its headed. In one of the posts, someone mentioned using exec > myname.log 2>&1 inside the script, but that resulted in an "Ambiguous redirection" error message.

Thanks
\naren
 
Old 05-14-2008, 04:46 PM   #4
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
is myscript.sh a script you've written or have the ability to modify?

If you have to redirect output to get it to a file (from the command line), then the default output is stdout - which usually comes from something like echo.

So simply do 2 echos, i.e.

Code:
echo $foo
echo $foo >> constant_log_file
This way the user can do what they want with stdout & it will not have an effect on what goes to constant_log_file.

Otherwise, I don't believe it's possible for the script to know how the user has manipulated stdout other than to look for itself in the process tree to see how it was executed.
 
Old 05-14-2008, 06:37 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
Try this; at the top of your script call your main code as a subscript and use the tee cmd to take a copy of the stdout.
You might be able to call your main code as fns and get this same technique to work.
 
Old 05-15-2008, 04:40 PM   #6
adelie
LQ Newbie
 
Registered: May 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks! The tee solution is quite elegant and meets my needs.

Cheers
 
  


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
Some of the output redirected to /dev/null appears on the screen while 'scripting' gregorian Linux - Newbie 27 06-23-2008 09:44 AM
Expect script - No output to file pandersson61 Programming 4 05-23-2007 05:18 AM
init 3 ---> error output from wifi card is redirected to stdout koca Fedora 1 02-02-2007 08:01 AM
can output of tail be redirected? dsids Linux - Newbie 19 10-16-2006 02:19 AM
shell script output to file drum2jc Linux - Software 1 05-30-2006 06:54 PM


All times are GMT -5. The time now is 05:03 PM.

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