- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: exec redirection plus tee? possible?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-07-2003 06:26 AM
тАО03-07-2003 06:26 AM
exec 1>log 2>&1
...but now I'm wondering if there's any way to use that in combination with tee.
The end result that I want is:
- Only specify redirection once, at the beginning of the script.
- Always have stdout go to a log file.
- If the script was run on an interactive terminal, then also have stdout go to the terminal (and still to the logfile).
A simple function to both send a message to a log and to the terminal isn't enough - I need to send stdout both places.
I guess I could buffer all output into variables and then echo it, but that gets ugly.
I think what I really need is some way to attach stdout to two file descriptors and then redirect those file descriptors separately.
Possible? Suggestions?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-07-2003 06:29 AM
тАО03-07-2003 06:29 AM
Re: exec redirection plus tee? possible?
Have you ever looked at the script command
It will do what you ask.
You get normal i/o to the screen and a copy in a log.
Steve Steel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-07-2003 07:16 AM
тАО03-07-2003 07:16 AM
Re: exec redirection plus tee? possible?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-07-2003 07:25 AM
тАО03-07-2003 07:25 AM
Re: exec redirection plus tee? possible?
==========================
#!/usr/bin/ksh
exec 1>/tmp/ls.log 2>&1
ls |tee /dev/tty
==========================
this should log to the screen and to the file.
To see if you're in interactive mode, you can use the syntax:
if [ -t 0 ] ; then
...
...
fi
rgds, Robin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-07-2003 07:26 AM
тАО03-07-2003 07:26 AM
Re: exec redirection plus tee? possible?
You could experiment with 'tail -f' to display the logfile as it is written to your terminal.
Simple way:
You have to ctrl-C the tail command when the script finishes.
Possible scripted way, you may only want to do this if a certain flag is supplied to the script:
CHILD=""
exec 1>log 2>&1
if [[ -t 1 ]]; # controlling terminal exists
then tail -f
CHILD=${!}
fi
... body of script ...
Yourscript exit code to kill the tail:
if [[ -n ${CHILD} ]];
then kill ${CHILD}
fi
exit
Regards,
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-07-2003 08:40 AM
тАО03-07-2003 08:40 AM
Re: exec redirection plus tee? possible?
And if it did work, then I'd be back at the beginning, appending something to every command.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-07-2003 08:44 AM
тАО03-07-2003 08:44 AM
Re: exec redirection plus tee? possible?
Can't eyeball that one and tell if it'll work, but it's going down the only path I've been able to imagine so far - forking so that one process does the actual work and another displays the output on an interactive terminal. I was really hoping there was a simpler way. :-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-07-2003 05:44 PM
тАО03-07-2003 05:44 PM
Re: exec redirection plus tee? possible?
#!/usr/bin/ksh
logfile=
tty=$(tty)
exec 3> $tty
if [ -t = 0 ] ;then
/usr/local/bin/outputredirect $tty $logfile |&
exec >&p
else
exec 1>$logfile 2>&1
fi
rest of your script
file outputredirect
#!/usr/bin/ksh
tty=$1
logfile=$2
while read line
do
print $line >> $logfile
print $line > $tty
done
give that a try and see how it works for you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-09-2003 03:55 PM
тАО03-09-2003 03:55 PM
Re: exec redirection plus tee? possible?
logfile=$0.log
if [ -t 0 ]
then
tee $logfile |&
exec 1>&p 2>&p
else
exec 1>$logfile 2>&1
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-10-2003 07:22 AM
тАО03-10-2003 07:22 AM
Re: exec redirection plus tee? possible?
BTW, the bits about co-processes in the manual pages never made sense until now - thanks for being great teachers! :-)
Curt, when I first tested your method, I didn't think it worked - not sure how I goofed, but it does work perfectly, so if you want the rest of the points, just post again and I'll assign them.
Thanks for your help, everyone.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-10-2003 07:43 AM
тАО03-10-2003 07:43 AM
Solutionyou probably caught that i left out redirection of stderr to the coprocess.
/usr/local/bin/outputredirect $tty $logfile |&
exec >&p
should have been
/usr/local/bin/outputredirect $tty $logfile |&
exec >&p 2>&p
the file outputredirect works using the tee command also:
#!/usr/bin/ksh
tee -a $2 >$1
but I can not tell you why it works in a seperate file but not as part of the shell script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-10-2003 07:50 AM
тАО03-10-2003 07:50 AM
Re: exec redirection plus tee? possible?
/usr/bin/ksh -c tee -a $logfile >$tty |&
that way everything can just be in the one shell script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-10-2003 11:45 AM
тАО03-10-2003 11:45 AM
Re: exec redirection plus tee? possible?
Oh, I'm being stupid. stdout from the co-process returns to the parent script. This means that, in this case, tee is writing to the log file, but also back to the script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-10-2003 12:04 PM
тАО03-10-2003 12:04 PM
Re: exec redirection plus tee? possible?
Got it! Simple fix. You must also tell tee to send stdout to stderr (aka tty) when becoming a co-proc so it doesn't map back to the returning pipe.
logfile=$0.log
if [ -t 0 ]
then
tee $logfile >&2 |&
exec >&p 2>&1
else
exec >$logfile 2>&1
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-10-2003 12:06 PM
тАО03-10-2003 12:06 PM
Re: exec redirection plus tee? possible?
Curtis' method accomplishes the same task.