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 07-13-2008, 03:59 PM
"Stefan Schmidt"
 
Default Bash: trap vs. tee

Hello,

I have a bash script with trap, but the trap doesn't catch the error.

function handle {
echo "This should be reached
exit 1
}
trap handle ERR
ls nonexist | tee -a output.log
echo "This should not be reached"

outputs

ls: nonexist: No such file or directory
This should not be reached

How come?

Stefan


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 07-13-2008, 04:17 PM
Sven Joachim
 
Default Bash: trap vs. tee

On 2008-07-13 17:59 +0200, Stefan Schmidt wrote:

> I have a bash script with trap, but the trap doesn't catch the error.
>
> function handle {
> echo "This should be reached

Closing quotes are missing, but your script hopefully has them.

> exit 1
> }
> trap handle ERR
> ls nonexist | tee -a output.log
> echo "This should not be reached"
>
> outputs
>
> ls: nonexist: No such file or directory
> This should not be reached
>
> How come?

Because the exit status of the pipe is that of the last command, and tee
has no reason to complain:

,----
| % ls nonexistent | tee -a output.log
| ls: cannot access nonexistent: No such file or directory
| % echo $?
| 0
| %
`----

In bash you can change this behavior with "set -o pipefail".

Sven


--
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 07:12 AM.

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