Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


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

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


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 ?

Old 05-14-2008, 02:28 PM   #2
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, 04:49 PM   #3
LQ Newbie
Registered: May 2008
Posts: 3

Original Poster
Rep: Reputation: 0
I have a shell script: >& 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.

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

Rep: Reputation: 51
is 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.

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, 07:37 PM   #5
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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, 05:40 PM   #6
LQ Newbie
Registered: May 2008
Posts: 3

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



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 10:44 AM
Expect script - No output to file pandersson61 Programming 4 05-23-2007 06:18 AM
init 3 ---> error output from wifi card is redirected to stdout koca Fedora 1 02-02-2007 09:01 AM
can output of tail be redirected? dsids Linux - Newbie 19 10-16-2006 03:19 AM
shell script output to file drum2jc Linux - Software 1 05-30-2006 07:54 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:02 PM.

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