X-Spam-Check-By: sourceware.org Date: Tue, 26 Jun 2007 23:40:48 -0400 (EDT) From: Igor Peshansky Reply-To: cygwin AT cygwin DOT com To: Richard A Nelson cc: cygwin AT cygwin DOT com Subject: Re: Bash is broken wrt trap :( In-Reply-To: <4681CDCD.7030709@linux.vnet.ibm.com> Message-ID: References: <4681CDCD DOT 7030709 AT linux DOT vnet DOT ibm DOT com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com 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 AT cs DOT nyu DOT edu | igor AT watson DOT ibm DOT 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/