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: d...@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.

_______________________________________________
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"

Reply via email to