Operating System - HP-UX
1753272 Members
4874 Online
108792 Solutions
New Discussion юеВ

redirection in a shell script?

 
SOLVED
Go to solution
Trever Furnish
Regular Advisor

redirection in a shell script?

Hopefully this will be a simple question for someone.

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.
Hockey PUX?
10 REPLIES 10
Paula J Frazer-Campbell
Honored Contributor

Re: redirection in a shell script?

Trevor

How about:-

script >> logfile

You will have to echo out each result.

Paula
If you can spell SysAdmin then you is one - anon
Sridhar Bhaskarla
Honored Contributor

Re: redirection in a shell script?

Hi Trever,

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
You may be disappointed if you fail, but you are doomed if you don't try
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: redirection in a shell script?

You need to check out the 'exec' statement. Man sh_posix for details.

exec 1>/tmp/myfile
exec 2>>/tmp/myerrs

Stdout is redirected to /tmp/myfile
Stderr is redirected to /tmp/myerrs in append mode
If it ain't broke, I can fix that.
Deshpande Prashant
Honored Contributor

Re: redirection in a shell script?

Hi
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.
Take it as it comes.

Re: redirection in a shell script?

Hi,

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
Accept or Kudo
Trever Furnish
Regular Advisor

Re: redirection in a shell script?

It figures - you ponder something forever, then the second you ask someone else to think about it you figure something out that works.

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

Hockey PUX?
hpuxrox
Respected Contributor

Re: redirection in a shell script?

You could use a function to do this

#! /bin/sh

Function1 () {

command1
command2
commadn3
}

Function1 > file
Trever Furnish
Regular Advisor

Re: redirection in a shell script?

Wow.

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.
Hockey PUX?
harry d brown jr
Honored Contributor

Re: redirection in a shell script?

Trevor,

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
Live Free or Die