Operating System - Linux
1748116 Members
3481 Online
108758 Solutions
New Discussion юеВ

Output redirection help in ksh script

 
SOLVED
Go to solution
Mike Smith_33
Super Advisor

Output redirection help in ksh script

I am running a backup script and I would like the output saved in a log file. I would appreciate a little help. Here is the actual backup line

{ fbackup $backup_type -u $verbose_option $nfs_option -g $graph_file $index_opt
} $logfile 1> $logfile

Here is what it looked like when it ran using (ksh -x scriptname )

+ 0< /bow/resp + fbackup -0 -u -n -g /bow/bowgraph -I /bow/backuplogs/25Apr06.full -c /bow/backup_config -d /bow/fbackupfiles/datesm (*output cut off*)
fbackup(1004): session begins on Tue Apr 25 11:01:25 2006
fbackup(3203): volume 1 has been used 7 time(s)
fbackup(3024): writing volume 1 to the output file /dev/rmt/0m
fbackup(3009): WARNING: File number 72630 (/orabackup/oradata/ediprod/redologs/ediprod_redo2.dbf)
was not successfully backed up
fbackup(3055): total file blocks read for backup: 25546882
fbackup(3056): total blocks written to output file /dev/rmt/0m: 26110005
fbackup(1030): warnings encountered during backup


I wanted the above output to go into the file pointed to by $logfile. I pointed standard output and stantard error to $logfile which was empty after the job ran. $logfile and all other variables in the above line are defined earlier in the script.

Any suggestions?
15 REPLIES 15
harry d brown jr
Honored Contributor

Re: Output redirection help in ksh script

{ fbackup $backup_type -u $verbose_option $nfs_option -g $graph_file $index_opt
} &1 1> $logfile

live free or die
harry d brown jr
Live Free or Die
Peter Nikitka
Honored Contributor

Re: Output redirection help in ksh script

Hi,

you are using different redirections to the same file - which one will win?

Configure the output an error stream instead:

1) use different files for stdout and stderr
{
cmd
} >outfile 2>errorfile

2) Redirect one stream to the other
{
cmd
} >outfile 2>&1

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Rodney Hills
Honored Contributor
Solution

Re: Output redirection help in ksh script

If you are running in a script, you have the option to assign STDOUT and STDERR before the fbackup command and thus capture all output. Example-

exec >$logfile 2>&1
date
fbackup ...
date

HTH

-- Rod Hills
There be dragons...
James R. Ferguson
Acclaimed Contributor

Re: Output redirection help in ksh script

Hi Mike:

Order is important here, too!

Run this script snippet:

# cat .redir
#!/usr/bin/sh
echo "from stdout"
print -u2 "from stderr"
exit 0

...Do this:

# ./redir 2>&1 1> /redir.log
from stderr
# cat redir.log
from stdout


Now:

# ./redir 1> /redir.log 2>&1
# cat redir.log
from stdout
from stderr

In the second case you achieve what you want. In the first case, STDERR was assigned to your terminal *and* then STDOUT was assigned to the file.

Regards!

...JRF...

#
Mike Smith_33
Super Advisor

Re: Output redirection help in ksh script

Ok, so for I have tried what Harry recommended, the backups ran sucessfully so there were no errors but still I got output to the screen instead of to the file.

Actual code:
{ fbackup $backup_type -u $verbose_option $nfs_option -g $graph_file $index_opt
} &1 1>$logfile


Actual output

+ 0< /bow/resp 2>& 1 + fbackup -0 -u -n -g /bow/bowgraph -I /bow/backuplogs/25Apr06.full -c /bow/backup_config -d /bow/fbackupfilesm
fbackup(1004): session begins on Tue Apr 25 13:04:09 2006
fbackup(3203): volume 1 has been used 8 time(s)
fbackup(3024): writing volume 1 to the output file /dev/rmt/0m
fbackup(3055): total file blocks read for backup: 25676534
fbackup(3056): total blocks written to output file /dev/rmt/0m: 26241878


The output file is completely empty. Did I miss something?
Rodney Hills
Honored Contributor

Re: Output redirection help in ksh script

You can do as I suggested. But the error is you assign 2 before 1. Reverse them...

>$logfile 2>&1

HTH

-- Rod Hills
There be dragons...
James R. Ferguson
Acclaimed Contributor

Re: Output redirection help in ksh script

Hi (again) Mike:

With regard to your second question, see my post just above: "Order is important here, too!"

Regards!

...JRF...

Mike Smith_33
Super Advisor

Re: Output redirection help in ksh script

James, I am running your script tests now and I am beginning to get what you are saying. It looks like I just need to change the order. I have a meeting in 6 minutes, I will retest after that.
Mike Smith_33
Super Advisor

Re: Output redirection help in ksh script

Just got a chance to try James' suggestion and here is what I got.

{ fbackup $backup_type -u $verbose_option $nfs_option -g $graph_file $index_option -c $config_file -d $fbackupfiles -f $device
} $logfile 2>&1


fbackup(1004): session begins on Tue Apr 25 15:02:37 2006
fbackup(3203): volume 1 has been used 9 time(s)
fbackup(3024): writing volume 1 to the output file /dev/rmt/0m
fbackup(3055): total file blocks read for backup: 21220083
fbackup(3056): total blocks written to output file /dev/rmt/0m: 21787601


Still no output in the file. I will now try the method given by Rodney.