Output redirection help in ksh script (265 Views)
Reply
Super Advisor
Mike Smith_33
Posts: 222
Registered: ‎11-18-2004
Message 1 of 16 (265 Views)
Accepted Solution

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?
Honored Contributor
harry d brown jr
Posts: 8,418
Registered: ‎12-12-2000
Message 2 of 16 (265 Views)

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
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 3 of 16 (265 Views)

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"
Honored Contributor
Rodney Hills
Posts: 1,679
Registered: ‎09-09-1996
Message 4 of 16 (265 Views)

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...
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 16 (265 Views)

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...

#
Super Advisor
Mike Smith_33
Posts: 222
Registered: ‎11-18-2004
Message 6 of 16 (265 Views)

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?
Honored Contributor
Rodney Hills
Posts: 1,679
Registered: ‎09-09-1996
Message 7 of 16 (265 Views)

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...
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 16 (265 Views)

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...

Super Advisor
Mike Smith_33
Posts: 222
Registered: ‎11-18-2004
Message 9 of 16 (265 Views)

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.
Super Advisor
Mike Smith_33
Posts: 222
Registered: ‎11-18-2004
Message 10 of 16 (265 Views)

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.
Honored Contributor
Rodney Hills
Posts: 1,679
Registered: ‎09-09-1996
Message 11 of 16 (265 Views)

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...
Super Advisor
Mike Smith_33
Posts: 222
Registered: ‎11-18-2004
Message 12 of 16 (265 Views)

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
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 13 of 16 (265 Views)

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...

Super Advisor
Mike Smith_33
Posts: 222
Registered: ‎11-18-2004
Message 14 of 16 (265 Views)

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.
Trusted Contributor
Christian Tremblay
Posts: 246
Registered: ‎06-25-2000
Message 15 of 16 (265 Views)

Re: Output redirection help in ksh script

shouldn't your redirection line be:

2>&1 >> $logfile
Super Advisor
Mike Smith_33
Posts: 222
Registered: ‎11-18-2004
Message 16 of 16 (265 Views)

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.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.