Print Error in Console and both Error & Output in Log file - UNIX

I am writing 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. But i am facing the below error.

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

Error Message : Currently the 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 dont want to use set -x option.

Dear sarathy_a35,

You have received at least three reminders to use code tags and format your questions so our members can easily read them. You seem to have decided that the forum rules, which you agreed to when you signed up, do not apply to you. If you continue to post without respecting the community rules and guidelines we will have no choice but to change your status to read only.

This is not personal. We apply our rules and guidelines equally to everyone. Most people follow the rules and enjoy formatting their posts so other members can easily read them.

1 Like