Can't figure out recursion problem in bash/freebsd

Bernard T. Higonnet bth at
Thu Jan 7 22:02:49 UTC 2010

Dear freebsd list,

There must be something simple I'm getting wrong in attempting a simple 
recursive bash script.

I want to do something on every file in a tree, so I have a script which 
recurses when it finds a directory and processes the file when it is not 
a directory.

My testbed is a directory with 3 subdirectories and a few files. My 
script correctly recognizes the first directory encountered, recurses to 
process that sub-directory, but when it comes back it no longer 
recognizes the next item as a directory, though it is indeed a 
directory. In the script given below, the processing of an actual file 
is represented by "echo processing $hoo".

If I eliminate the recursion but enter and then exit the sub-directory 
(i.e. I replace line "$0" with "cd ..") the sub-directories are 
correctly recognized.

echo starting in `pwd`
for hoo in *
     echo $hoo
     if [ -d "$hoo" ]
       then echo pushing $hoo; cd $hoo
       else echo processing $hoo
   echo going to next item
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

More information about the freebsd-questions mailing list