I have found a way to get the desired output. Though it may be somewhat unorthodox way. Anyways here it goes. In the redir.env file I have following code:
#####redir.env#####
export LOG_FILE=log.txt
exec 2>>${LOG_FILE}
function log {
echo "$1">>${LOG_FILE}
}
function message {
echo "$1"
echo "$1">>${LOG_FILE}
}
Then in the actual script I have the following codes:
#!/bin/sh
. redir.env
echo "Echoed to console only"
log "Written to log file only"
message "To console and log"
echo "This is stderr. Written to log file only" 1>&2
Here echo outputs only to console, log outputs to only log file and message outputs to both the log file and console.
After executing the above script file I have following outputs:
In console
In console
Echoed to console only
To console and log
For the Log file
In Log File Written to log file only
This is stderr. Written to log file only
To console and log
Hope this help.