Redirecting output of Make to file

Hi,

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

# 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"

Regards
Srujan

---------- Post updated at 06:28 PM ---------- Previous update was at 06:26 PM ----------

Oh, if it is not clear - the entire code snippet I have mentioned above is the shell script itself (meaning, the make command is part of the script itself)

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:

#!/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.

The "&>" will actually redirect all output (stderr, stdout) in this case to a file.

1 Like

&> is bash-only..

1 Like