- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- redirection in a shell script?
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
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
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
тАО05-02-2002 08:13 AM
тАО05-02-2002 08:13 AM
In the HP posix shell (man sh-posix), is it possible to set output redirection at the beginning of a script and have that redirection apply to all subsequent lines in the script?
For example, instead of this:
#!/bin/sh
line1 >>/my/log.file
line2 >>/my/log.file
line3 >>/my/log.file
...I'd like to do somethine like this:
#!/bin/sh >>/my/log.file
line1
line2
line3
That particular method doesn't work and I can't find another one that does either. This would *really* clean up my scripts a lot.
I know I could redirect the output for the script itself when I invoke it, but there's always the possibility that if the script exits abnormally I won't get output at all - which defeats the whole purpose.
In Perl I could accomplish this with a simple re-opening of the standard output filehandle like so (example taken from the perlfunc man page):
open(STDOUT, '>', "foo.out") || die "Can't redirect stdout";
open(STDERR, ">&STDOUT") || die "Can't dup stdout";
That would attach STDOUT for the current process to foo.out and then duplicate STDOUT as STDERR.
It seems like there *has* to be some way to do it in a shell language, but I'm missing it...
Help? If anyone needs clarification, just let me know.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2002 08:16 AM
тАО05-02-2002 08:16 AM
Re: redirection in a shell script?
How about:-
script >> logfile
You will have to echo out each result.
Paula
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2002 08:18 AM
тАО05-02-2002 08:18 AM
Re: redirection in a shell script?
You can keep the commands in {} to combine the output. For ex., your script would do the following
#!/sbin/sh
...
...
{
line 1
line 2
line 3
} > /my/log.file
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2002 08:18 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2002 08:19 AM
тАО05-02-2002 08:19 AM
Re: redirection in a shell script?
I use following method.
##-----
#Define common variables ##
HP=`hostname`
LOGFILE=/home/system/LOGS/${HP}-Log
exec 2>&1 >>${LOGFILE}
## start commands
command1.
command2.
...
##-----
Thanks.
Prashant Deshpande.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2002 08:19 AM
тАО05-02-2002 08:19 AM
Re: redirection in a shell script?
use exec (see man sh-posix) :
---------------------------
#!/usr/bin/sh
exec 1>mylog
echo hello
echo more
exit
---------------------------
In the above, all stdout will go to mylog
HTH
Duncan
I am an HPE Employee
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2002 08:20 AM
тАО05-02-2002 08:20 AM
Re: redirection in a shell script?
But this isn't very elegant at all, so I'd still love to hear it if there's a better way...
Here's the work-around I thought of: Run the script with another script as a wrapper - redirect the output of the internal script to a temporary file. When the internal script is done, then mail/archive/whatever the log file. This gets around a "broken pipe" losing data because the log file writes happen immediately rather than at the end of the process as pipes do.
Actually, I think I gave a bad example initially - instead of writing to a log file, I'm more worried about the case of losing data where I would normally be executing a sub-process and sending it to a pipe, as in this crontab entry example:
0 0 * * * (date; bdf) 2>&1 | /usr/bin/mail -s "bdf" foo@bar.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2002 08:25 AM
тАО05-02-2002 08:25 AM
Re: redirection in a shell script?
#! /bin/sh
Function1 () {
command1
command2
commadn3
}
Function1 > file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2002 08:27 AM
тАО05-02-2002 08:27 AM
Re: redirection in a shell script?
Access to the ITRC forums make HP's premium prices well worth it. :-) By the time I'd posted my second message in this article five of you had already replied.
Exec. And it's right there in the man page. Color me embarrassed. But also very grateful.
Many thanks to all. :-) I'll assign points shortly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2002 08:53 AM
тАО05-02-2002 08:53 AM
Re: redirection in a shell script?
I myself just learned something new (exec 1>>file), I wonder if my boss will let me go home now....yeah right
live free or die
harry