This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Bash is broken wrt trap :(


On Tue, 26 Jun 2007, Richard A Nelson wrote:

> Cygwin bash is actually a newer version than Debian/RH ships - but has
> one flaw not shared by the other distributions.
>
> I have code that must run on AIX (bash/ksh93), Linux (bash/ksh93), and
> Cygwin (bash - pdksh is not sufficient and I couldn't find ksh93)...

What's missing in pdksh?

> and trap handling is a large part of what the scripts do.
>
> Here is the main trap setup:
> -------------------------------------------------------------------------
> # Set a trap to catch non-zero return codes
> ERROR="*** ERROR *** ERROR *** ERROR ***";
> trap "echo \"$0:$ERROR\";" ERR;
> if [ -n "$BASH" -a "${OS:0:6}" != 'CYGWIN' ]; then

This won't work -- OS is a variable set by Windows.  On my WinXP, I get
OS=Windows_NT, so ${OS:0:6} is 'Window'.

You want something like "$(uname -s | cut -c 1-6)" instead, or use
"$(uname -o)".

>         UNWIND="trap 'false' RETURN;return 1";

I'm not sure this does what you expect it to.  It seems like this sets the
RETURN trap *in the child*, which is executed, again in the child, as soon
as you return.

> else
>         UNWIND='return 1';
>         fi;
> trap "echo \"$0:$ERROR\";$UNWIND" ERR;
> set -e;
> ------------------------------------------------------------------------
>
> Each function sets its own trap handling:
> Parse_Options () {
>         trap "echo \"$0/Options:$ERROR\";$UNWIND" ERR;
>         ...
>         }
>
> Here is the test driver I'm working with
> -------------------------------------------------------------------------
> trap "echo \"$0: $ERROR\";exit 1" ERR QUIT ABRT ALRM TERM;
>
> do_a () {
>         trap "echo \"$0/do_a:  $ERROR\";$UNWIND" ERR;
>         echo '>>do_a';
>         do_b;
>         }
>
> do_b () {
>         trap "echo \"$0/do_b:  $ERROR\";$UNWIND" ERR;
>         echo '>>do_b';
>         false;
>         }
>
> echo '>>main'
> do_a;
> --------------------------------------------------------------------------
>
> ksh93 will propagate the error upwards via the 'return 1' clause.
> bash, however requires setting a RETURN trap and forcing an error
> in the function's caller :(

And what is the output you expect from the above script?  That every error
handler up the call chain is triggered?

> Anyway this works everywhere except Cygwin, as you can see I've disabled
> the RETURN TRAP for Cygwin, as with it enabled, the script loops forever
> (or until it faults) - continually reissuing the trap in do_b !
> --------------------------------------------------------------------------
> + echo '>>main'
> >>main
> + do_a
> + trap 'echo "./test/do_a:  *** ERROR *** ERROR *** ERROR ***";trap
> '\''false'\
> + echo '>>do_a'
> >>do_a
> + do_b
> + trap 'echo "./test/do_b:  *** ERROR *** ERROR *** ERROR ***";trap
> '\''false'\
> + echo '>>do_b'
> >>do_b
> + false
> ++ echo './test/do_b:  *** ERROR *** ERROR *** ERROR ***'
> ./test/do_b:  *** ERROR *** ERROR *** ERROR ***
> ++ trap false RETURN
> ++ return 1
> +++ false
> ++++ echo './test/do_b:  *** ERROR *** ERROR *** ERROR ***'
> ./test/do_b:  *** ERROR *** ERROR *** ERROR ***
> ++++ trap false RETURN
> ++++ return 1
> +++++ false
> [snip]

	Igor
-- 
				http://cs.nyu.edu/~pechtcha/
      |\      _,,,---,,_	    pechtcha@cs.nyu.edu | igor@watson.ibm.com
ZZZzz /,`.-'`'    -.  ;-;;,_		Igor Peshansky, Ph.D. (name changed!)
     |,4-  ) )-,_. ,\ (  `'-'		old name: Igor Pechtchanski
    '---''(_/--'  `-'\_) fL	a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!

Freedom is just another word for "nothing left to lose"...  -- Janis Joplin

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]