From owner-freebsd-questions@FreeBSD.ORG Thu Jan 7 23:12:02 2010 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 270C51065672 for ; Thu, 7 Jan 2010 23:12:02 +0000 (UTC) (envelope-from djr@pdconsec.net) Received: from ipmail03.adl2.internode.on.net (ipmail03.adl2.internode.on.net [203.16.214.135]) by mx1.freebsd.org (Postfix) with ESMTP id A5D098FC13 for ; Thu, 7 Jan 2010 23:12:01 +0000 (UTC) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApoEAPj2RUuWZcBC/2dsb2JhbADUWoItggIE Received: from goliath.pdconsec.net (HELO smtp.pdconsec.net) ([150.101.192.66]) by ipmail03.adl2.internode.on.net with SMTP; 08 Jan 2010 09:41:58 +1030 Received: from mail1.pdconsec.net ([192.168.1.41] helo=mail1.pdconsec.net) with IPv4:25 by smtp.pdconsec.net; 8 Jan 2010 10:16:08 +1100 Received: from smtp.pdconsec.net ([192.168.1.32] RDNS failed) by mail1.pdconsec.net with Microsoft SMTPSVC(6.0.3790.3959); Fri, 8 Jan 2010 10:11:39 +1100 Received: from [10.14.6.41] ([150.101.192.69] helo=[10.14.6.41]) with IPv4:25 by smtp.pdconsec.net; 8 Jan 2010 10:16:07 +1100 Message-ID: <4B466A2B.6030906@pdconsec.net> Date: Fri, 08 Jan 2010 10:11:39 +1100 From: David Rawling User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091204 Thunderbird/3.0 MIME-Version: 1.0 To: freebsd-questions@freebsd.org References: <4B46566C.4050705@higonnet.net> In-Reply-To: <4B46566C.4050705@higonnet.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 07 Jan 2010 23:11:39.0946 (UTC) FILETIME=[C4C120A0:01CA8FEE] Subject: Re: Can't figure out recursion problem in bash/freebsd X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 07 Jan 2010 23:12:02 -0000 On 8/01/2010 8:47 AM, Bernard T. Higonnet wrote: > echo starting in `pwd` > for hoo in * > do > echo $hoo > if [ -d "$hoo" ] > then echo pushing $hoo; cd $hoo > $0 > else echo processing $hoo > fi; > echo going to next item > done > cd .. > > I have tried various minor variations , all to no avail. > > I have no doubt I'm doing something very dumb, but I'm too locked into > my vision to see it... > > All help appreciated > Bernard Higonnet I am probably the last person you'd want debugging your scripts, but I can at least reproduce the problem. My test folder and file structure: /tmp/test dir0 dir00 file00 file0 dir1 dir11 file11 file1 Luckily, I think I have also derived the solution. The problem appears to be the directory stack. Specifically, the output of my revised version shows that it's not working in the right folders all the time. #! /bin/sh echo Starting in `pwd` for hoo in *; do echo $hoo if [ -d "$hoo" ]; then echo Pushing $hoo; cd $hoo ($0) else echo Processing file $hoo fi echo Going to next item done cd .. echo Finishing in `pwd` By moving the cd command into the if statement, we change back into the correct folder at the right time (otherwise the siblings to the first directory cannot be found in the for loop, perhaps because the current directory has changed mid-execution): test01# cat /root/recurse.sh #! /bin/sh echo Starting in `pwd` for hoo in *; do echo Found item $hoo if [ -d "$hoo" ]; then echo Pushing $hoo cd $hoo $0 cd .. else echo Processing file $hoo fi echo Going to next item done echo Finishing in `pwd` test01# I think it works - someone brighter than me can tell us both why :). Most of the changes you see there are stylistic (eg the placement of then/else and do/done) or were for my own clarity in figuring out what was being printed where. Dave. -- David Rawling Principal Consultant PD Consulting And Security 7 Virginia Ave Baulkham Hills, NSW 2153 Australia Mob: +61 412 135 513 Email: djr@pdconsec.net Please note that whilst we take all care, neither PD Consulting and Security nor the sender accepts any responsibility for viruses and it is your responsibility to scan for viruses. The contents are intended only for use by the addressee and may contain confidential and/or privileged material and any use by other than the intended recipient is prohibited. If you received this in error, please inform the sender and/or addressee immediately and delete the material.