delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2007/06/27/02:34:48

X-Spam-Check-By: sourceware.org
Message-ID: <468204ED.2040603@linux.vnet.ibm.com>
Date: Tue, 26 Jun 2007 23:34:21 -0700
From: Richard A Nelson <cowboy AT linux DOT vnet DOT ibm DOT com>
User-Agent: Mozilla-Thunderbird 2.0.0.4 (X11/20070622)
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: Re: Bash is broken wrt trap :(
References: <4681CDCD DOT 7030709 AT linux DOT vnet DOT ibm DOT com> <Pine DOT GSO DOT 4 DOT 63 DOT 0706262321120 DOT 10083 AT access1 DOT cims DOT nyu DOT edu>
In-Reply-To: <Pine.GSO.4.63.0706262321120.10083@access1.cims.nyu.edu>
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

--------------000808060705060000060106
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

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


--------------000808060705060000060106
Content-Type: text/plain;
 name="sample"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="sample"

#!/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=$?";



--------------000808060705060000060106
Content-Type: text/plain; charset=us-ascii

--
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/
--------------000808060705060000060106--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019