delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2003/07/30/15:26:30

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/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
X-Injected-Via-Gmane: http://gmane.org/
To: cygwin AT cygwin DOT com
From: Andrew DeFaria <ADeFaria AT Salira DOT com>
Subject: Bash traps - while emitting ERR trap
Date: Wed, 30 Jul 2003 12:15:07 -0700
Lines: 49
Message-ID: <bg95fj$tml$1@main.gmane.org>
Mime-Version: 1.0
X-Complaints-To: usenet AT main DOT gmane DOT org
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)
X-Accept-Language: en-us, en, zh

Given the following script:

#!/bin/bash
function cleanup {    echo "ENTER cleanup"
} # cleanup
                   trap cleanup EXIT ERR
            declare -i i=4           echo "Entering while loop"
while [ $i -gt 0 ]; do
 echo "In while loop"
 let i=i-1
done                   echo "After trap is set"
Why is "ENTER cleanup" echoed twice when run?

$ traptest
Entering while loop
In while loop
In while loop
In while loop
In while loop
ENTER cleanup
After trap is set
ENTER cleanup

If I remove the EXIT from the trap statement then I get the following

Entering while loop
In while loop
In while loop
In while loop
In while loop
ENTER cleanup
After trap is set

and if I omit the ERR from the trap statement I get the following:

Entering while loop
In while loop
In while loop
In while loop
In while loop
After trap is set
ENTER cleanup

It seems as though the EXIT trap is getting invoked properly when the 
script exits but the ERR trap is being generated upon exit of the while 
loop. Is this correct behavior? If is it correct behavior then how can I 
script a cleanup routine to trap properly for such things as EXIT ERR 
and INT in the presence of while loops?



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

- Raw text -


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