Redirecting Output To File
Unable to get this script to work as desired. Basically,
if an argument "log" is sent into the script, it outputs the result of
the Make to a file output.log. However, if the argument is not passed,
I want the output to be just put on screen (no redirection). See code snippet
below.
Code:
# program name redirect_make.sh
# Setup
FILE=
OUTPUTFILE="output.log"
if [ "$1" == 'log' ]
then
{
FILE = $OUTPUTFILE
}
fi
# Now make and redirect all output to file (including
errors/warnings)
# if output file has been specified, else do not
redirect (print to screen)
make -f program.mak &> "$FILE"
Resolution:
Not sure what shell you are using (&> as redirection
isn't anything I recognse) and that might be some/all of the problem.
The other issue might be that if no log name is given
on the command line redirection is to an empty string which should generate
an error.
Something like this might solve the issue:
Code:
#!/usr/bin/env ksh
# program name redirect_make.sh
# Setup
FILE="/dev/fd/1"
# default to output to tty
OUTPUTFILE="output.log"
if [ "$1" == 'log' ]
then
FILE = $OUTPUTFILE
fi
# Now make and redirect all output to file (including
errors/warnings)
# if output file has been specified, else do not redirect
(print to screen)
make -f program.mak >$FILE 2>&1
/dev/fd/1 is supported by ksh; not sure whether or not
bash does.
Note:
&> is bash-only
The "&>" will actually redirect all output (stderr,
stdout) in this case to a file.
Also:
Look into 'man tee'
Have a Unix Problem
Unix
Forum - Do you have a UNIX Question?
Unix Books :-
UNIX Programming,
Certification, System Administration, Performance Tuning Reference Books
Return to : - Unix
System Administration Hints and Tips
(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.
Information used on this site is at your own risk.
All product names are trademarks of their respective
companies.
The site www.gotothings.com is in no way affiliated with
or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.
|