FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 12-14-2007, 06:56 PM
"Tony Heal"
 
Default 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"


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 12-15-2007, 02:52 AM
"Douglas A. Tutty"
 
Default Send stderr to variable, but send stdout to screen?

On Fri, Dec 14, 2007 at 02:56:36PM -0500, Tony Heal wrote:
> 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.

> # Capture STDERR to ERR
> ERR=$($CMD 2>&1)

2>&1 puts stderr (2) and adds that stream to stdout; they get mixed.
So, you can't see stdout. Just get 2 and ignore 1.

More detailed than that I can't help. I don't do these scripts in sh.
I would use python.

Doug.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 12-15-2007, 10:27 AM
Raj Kiran Grandhi
 
Default Send stderr to variable, but send stdout to screen?

Tony Heal wrote:

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.


Try this:

$ errors=$(command 2>&1 1> $(tty))


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 12-15-2007, 03:21 PM
"Tony Heal"
 
Default Send stderr to variable, but send stdout to screen?

Works great. Thanks

Tony

-----Original Message-----
From: Raj Kiran Grandhi [mailto:grajkiran@gmail.com]
Sent: Saturday, December 15, 2007 6:27 AM
To: Tony Heal
Cc: debian-user@lists.debian.org
Subject: Re: Send stderr to variable, but send stdout to screen?

Tony Heal wrote:
> 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.

Try this:

$ errors=$(command 2>&1 1> $(tty))


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 

Thread Tools




All times are GMT. The time now is 12:23 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org