Operating System - HP-UX
1836503 Members
1943 Online
110101 Solutions
New Discussion

saving out put to file - a better way ??

 
SOLVED
Go to solution
someone_4
Honored Contributor

saving out put to file - a better way ??

I am writtinga script and I want to save the out put to a file. This is how I am doing it now. I use >> to apend to the file.
Is there a better way to do this then using
>> on everyline ?
------------------
echo "HP9000" >> daily.txt
remsh boxname -l root "tail -n30 /var/adm/syslog/syslog.log" >> daily.txt
----------------
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: saving out put to file - a better way ??

Hi Richard:

Yes, duplicate the stdout descriptor. Try this:

#!/usr/bin/sh
exec >&1 >> /tmp/output
echo "It's $(date)"

Notice that all your output goes to the file /tmp/output without the need to redirect it as you go.

Regards!

...JRF...
Curtis Larson
Trusted Contributor

Re: saving out put to file - a better way ??

you can use command grouping

sub shell grouping:
(
your stuff
) >> file.out

your code will run in a sub shell environment which means there will be no side effects, ie if you change variables in the grouping once your out of the grouping your changes won't be there. If you do nesting you'll need to put white space between the parentheses to avoid arithmetic evaluation.

you can also do a brace grouping:

{
your stuff
} >> file.out
Maureen Gunkel
Trusted Contributor

Re: saving out put to file - a better way ??

Richard,

Another way to do it is to specify the output file when you run the script, like so:
./myscript >> daily.txt
Just a thought,
Mo
No matter where you go, there you are.
Dan Hetzel
Honored Contributor

Re: saving out put to file - a better way ??

Hi Richard,

James definitely showed you the way to go.

Nevertheless, in his example, standard error will not be redirected to the file as '>&1' comes before '>>'.
If you want both standard error and standard output to be redirected you could use the following constructs:
exec > /tmp/output 2>&1 # to redirect both in same file
exec > /tmp/output 2>/tmp/error # to redirect in different files


Best regards,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Rodney Hills
Honored Contributor

Re: saving out put to file - a better way ??

Sidenote-

Within the KSH you can write the output to a running program. Use "exec >&p". I use it in my spooler print drivers to write to jetdirect connected printers.

/opt/hpnp/hpnpf -x 192.16.0.10 |&
exec >&p

From then on, all stdout from the script will go to the stdin of program hpnpf.
There be dragons...
Mark Mitchell
Trusted Contributor

Re: saving out put to file - a better way ??

How about
| tee -a /usr/tmp/filename