tech-userlevel archive

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

Re: set -e again



>> [...set -e vs shell functions...]

> Does this achieve what you want?

> #!/bin/sh
> set -e
> f() {
> 	echo foo
> 	false
> 	echo bar
> }
> f
> [ $? ] || echo baz
> echo buzz

I'm not the original poster, but it does not achieve what I would
expect, which is

foo
baz
buzz

That is, I would expect/want -e to cause the function to return showing
failure.  Instead, it either does nothing or takes down the whole
shell.

This does achieve it, not surprisingly, but it's an ugly kludge; I
would say that neither the nested sh nor the nested set -e should be
necessary:

set -e
f() {
sh -c '
	set -e
	echo "foo $-"
	false
	echo bar
'
}
f || echo baz
echo buzz

/~\ The ASCII				  Mouse
\ / Ribbon Campaign
 X  Against HTML		mouse%rodents-montreal.org@localhost
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


Home | Main Index | Thread Index | Old Index