Simpler Navigation coming for Servers and Operating Systems
Coming soon: a much simpler Servers and Operating Systems section of the Community. We will combine many of the older boards, and you won't have to click through so many levels to get at the information you need. If you are looking for an older board and do not find it, check the consolidated boards, as the posts are still there.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

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
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.
Rodney Hills
Honored Contributor

Re: Output redirection help in ksh script

Are those curly brackets you are using?

If so then the syntax requires a ; before the closing right curly bracket.

HTH

-- Rod Hills
There be dragons...
Mike Smith_33
Super Advisor

Re: Output redirection help in ksh script

This one didn't work either but I am confused as to why, it made sense.

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


+ fbackup -0 -u -n -g /bow/bowgraph -I /bow/backuplogs/25Apr06.full -c /bow/backup_config -d /bow/fbackupfiles/dates -f /dev/rmt/0m
fbackup(1004): session begins on Tue Apr 25 15:18:36 2006
fbackup(3203): volume 1 has been used 10 time(s)
fbackup(3024): writing volume 1 to the output file /dev/rmt/0m
fbackup(1102): WARNING: unable to stat file /entsys/tmp/cos.003354981498422125.1385.6
fbackup(3005): WARNING: file number 33092 was NOT backed up
fbackup(1030): warnings encountered during backup
fbackup(3055): total file blocks read for backup: 21283397
fbackup(3056): total blocks written to output file /dev/rmt/0m: 21851691
James R. Ferguson
Acclaimed Contributor

Re: Output redirection help in ksh script

Hi (again) Mike:

As Rodney noted, a a final semicolon is required after the last statement in curly braces. I agree that you last post seems correct otherwise. That aside, I use this in one of my scripts:

# ( /usr/sbin/fbackup -f $FB_DEV -0 -u -v -g $FB_GRF -V $FB_VOL -c $FB_CNF 2>&1 ; echo $? > $FB_XIT ) | tee -ia $FB_LOG

The approach I that I took here uses a sub process (and yes, there is not a semicolon in this syntax) but writes STDOUT and STDERR to ${FB_LOG} as well as the terminal.

Regards!

...JRF...

Mike Smith_33
Super Advisor

Re: Output redirection help in ksh script

I have added the semi colon before the right bracket and still the output comes to the screen. From what I understand of the output and error redirection this should work

1>$logfile 2>&1

This fbackup command is inside a script and I am trying to save the output and/or error/warnings in the $logfile. I am apparently not seeing what is wrong with the syntax.
Christian Tremblay
Trusted Contributor

Re: Output redirection help in ksh script

shouldn't your redirection line be:

2>&1 >> $logfile
Mike Smith_33
Super Advisor

Re: Output redirection help in ksh script

Some of the earlier answers were correct, it was my mistake in that I should have been using $log_file instead of $logfile. I would have found the answer much sooner.

Thanks for all the help and for putting up with my typo.