Bash is broken wrt trap :(

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


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

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: sample
URL: <http://cygwin.com/pipermail/cygwin/attachments/20070627/d4037b33/attachment.ksh>
-------------- next part --------------
--
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