我有一个带有两个运行时参数的shell脚本。在执行过程中,如果出现任何错误,则需要将其重定向到错误文件并在控制台中。同样,错误和输出都需要重定向到日志文件。
当前,错误输出将被复制到err文件并也显示在控制台中。 但是在日志文件中,输出和错误消息被重定向了两次。
Code
#! /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
}