[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Send stderr to variable, but send stdout to screen?



I have an error handling function that traps, reports and exits on an error.
My problem is that I want to do all of that and I want to still see stdout.

Can anyone help with this? Here is the function. If I change sources.list to
have an invalid source it reports, but I do not see the progress when there
is no error.

#!/bin/bash
logfile=/root/upgrade.log

	# R1040 error handling
	function run()
	{
	    CMD=$1
		echo 
	    # Don't abort on errors
	    set +e
	    # Capture STDERR to ERR
	    ERR=$($CMD 2>&1)
	    # Capture return value from command
	    RETVAL=$?
	    # re-enable abort-on-errors
	    set -e

	    return $RETVAL
	}

	function errhandler()
	{
		echo "********** ERROR DETECTED in $CMD **********" | tee -a
$logfile
		echo "********** Error reported was \"$ERR\"" | tee -a
$logfile
		echo "Exiting script abnormally. The error above was
reported in log at $logfile"
		exit 1
	}

	trap errhandler ERR
	run 'apt-get update'
	# should not get past this on error
	echo "no"



Reply to: