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 :(


Igor Peshansky wrote:

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?

subscript references, for one (ie ${OS:0:6}), which is fairly common in the scripts (though, most all could be replaced with ${OS%...} or ${OS#...} )


Also, I could find no way to have pdksh propagate the errors all the way up the call chain.

The scripts have sourced common routines, and may also be called from other
scripts (so I have a combination of local, shared functions in the same process,
and subprocesses used to invoke a subshell/script).

An example of the structuring might be helpful; I have separate scripts that build each component of our product (compiler, runtime, sort, ICU, etc...).
The scripts may be run stand-alone, or from a higher level 'build-it-all' script.


Then, there are several common subroutine script files that are source in each and every build/test script to handle things like cron job scheduling, command line parsing, etc.

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'.

Eh? in Cygwin? Works just fine here, $OS is not marked readonly - tho the subscript fails on pdksh :(


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

uname -o doesn't carry across non-GNU toolchains (AIX) :( but this was just an example usage


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.

It is documented that the RETURN trap executes in the context of the parent,
after the completion of the child (though, in the case of sourced, or in file functions, there should be no parent/child process)


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

Exactly... $ ./sample OS=Linux bash=3.00.15(1)-release braceexpand on hashall on interactive-comments on posix on >>main >>do_a >>do_b ./sample/do_b: *** ERROR *** ERROR *** ERROR *** <<do_a ./sample/do_a: *** ERROR *** ERROR *** ERROR *** ./sample: *** ERROR *** ERROR *** ERROR ***

I've attached a small complete, working version to this mail

--
Rick

#!/bin/sh

echo "OS=$(uname -s)";
if [ -n "$BASH" ]; then
	echo "bash=$BASH_VERSION";
	set -o posix;
	set +o xtrace;
#   These cause failures on AIX bash :(
#	set -E;
#	set -T;
elif [ -n "$KSH_VERSION" ]; then
	echo "pdksh=$KSH_VERSION";
	set -o posix;
	set -o xtrace;
else
	echo "ksh93";
	fi;

# Display current settings
set -o | grep -e 'on$';

# Set a trap to catch non-zero return codes
ERROR="*** ERROR *** ERROR *** ERROR ***";
trap "echo \"$0:$ERROR\";" ERR;
if [ -n "$BASH" -a "${OS%-*}" != 'CYGWIN' ]; then
    UNWIND="trap 'false' RETURN;return 1";
else
    UNWIND='return 1';
    fi;
set -e;
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;
	echo '<<do_a';
	}

do_b () {
	trap "echo \"$0/do_b:  $ERROR\";$UNWIND" ERR;
	echo '>>do_b';
	false;
	echo '<<do_b';
	}

echo '>>main'
do_a;
echo "<<main, rc=$?";


--
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]