You can make the whole script a function like this:
main_function
{
do_things_here
}
main_function() {
do_things_here
}
then at the end of the script have this:
if [ -z $TERM ]
then
# if not run via terminal, log everything into a log file
main_function 2>&1 >> /var/log/my_uber_script.log
else
# run via terminal, only output to screen
main_function
fi
if [ -z $TERM ]; then
# if not run via terminal, log everything into a log file
main_function 2>&1 >> /var/log/my_uber_script.log
else
# run via terminal, only output to screen
main_function
fi
Alternatively, you may log everything into logfile each run and still output it to stdout by simply doing:
# log everything, but also output to stdout
main_function 2>&1 | tee -a /var/log/my_uber_script.log
# log everything, but also output to stdout
main_function 2>&1 | tee -a /var/log/my_uber_script.log