Bash is broken wrt trap :(

Richard A Nelson cowboy@linux.vnet.ibm.com
Wed Jun 27 02:39:00 GMT 2007


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/



More information about the Cygwin mailing list