Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , 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 Subject: Bash traps - while emitting ERR trap Date: Wed, 30 Jul 2003 12:15:07 -0700 Lines: 49 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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/