UNIX Log File Content - Duplication Issue

I have a shell script with 2 run time arguments. During the execution if i got any error, then it needs to redirected to a error file and in console. Also both error and output to be redirected to a log file.

Error output is getting copied to err file and getting displayed in console too. But in log file the output and error message it getting redirected twice. I am not willing to use set -x option.

#! /bin/sh
errExit ()
{
    errMsg=`cat $1.log >> "$1".err`
    cat "$1".err | tee /dev/fd/3    
   return
}
test()
{
logfile=$1
exec 3>&1 1>>"${logfile}".log 2>&1
echo "$1"
echo "$2"
echo " "
echo "#################################################"
echo "Hi Hello,This is first function"
echo "#################################################"
if [[ -z "$2" ]];
then 
errExit $logfile
return
else
#<some Commands Here>
fi
}
test_1()
{
logfile=$1
exec 3>&1 1>>"${logfile}".log 2>&1
echo "$1"
echo "$2"
echo " "
echo "#################################################"
echo "Hi Hello,This is second function"
echo "#################################################"
if [[ -z "$2" ]];
then 
errExit $logfile
return
else
#<some Commands Here>
fi
}

I see that you append the output to $logfile. Does it already exist when you run this?

Perhaps adding `date` to the Hi Hello echo statement might show you if this is from a previous run.

What output do you actually have from this?

Kind regards,
Robin

Closing thread as this looks like homework, but mostly because once again you posted twice knowing its against our rules and for that reason you will receive yet again a infraction...
Please continue here:
Print Error in Console and both Error & Output in Log file - UNIX

1 Like