X-Spam-Check-By: sourceware.org Message-ID: <4681CDCD.7030709@linux.vnet.ibm.com> Date: Tue, 26 Jun 2007 19:39:09 -0700 From: Richard A Nelson User-Agent: Mozilla-Thunderbird 2.0.0.4 (X11/20070622) MIME-Version: 1.0 To: cygwin AT cygwin DOT com Subject: Bash is broken wrt trap :( Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Score: -4.322 () ALL_TRUSTED,BAYES_00,DKIM_POLICY_SIGNSOME,TW_PD Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Id: 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 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)... 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 UNWIND="trap 'false' RETURN;return 1"; 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 :( 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 ++++++ 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 ++++++++++ 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 ... --------------------------------------------------------------------------------- -- Richard A Nelson (Rick) cowboy@(vnet|us).ibm.com Phone: 1-408-463-5584 Fax: 1-408-463-3873 COBOL Development IBM Silicon Valley Laboratory http://www.ibm.com/software/awdtools/cobol/ -- 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/