Recursive shell scripts

Eric M. Boehm emb978 at leah.Albany.Edu
Sun Jul 9 07:05:11 AEST 1989


In article <343 at dftsrv.gsfc.nasa.gov> setzer at nssdcs (William Setzer (IDM))
writes:
> I am trying to use the Bourne shell to write recursive shell scripts.
> The problem is that I can't get the positional parameters to pass
> correctly.  Here is what I've written.
> 
> --------------
> #!/bin/sh
> export PATH
> cd ${1:-`pwd`}
> for I in `ls`
> do
> if test -d $I; then
>    . $0 $I;
> else
>   echo `pwd`'/'$I
> fi
> done

The problem is that executing a script with a '.' is *not* the same as
executing the file. From "The Unix Programming Environment", page 90:

   "When a file is executing with '.', it is only superficially like
running a shell file. ... Another difference is that the file does not
receive command line arguments; instead, $1, $2 and the rest are empty."

I was able to get this to work by doing away with the '.' as follows:

#!/bin/sh
export PATH
cd ${1-`pwd`}
for I in `ls`
do
export I
if test -d $I
  then
    $0 $I;
  else
	echo `pwd`/$I
fi
done

I also tried to get it to work using the '.', but the problem is that
when you start descending directories, there is no easy way to get back.
It might be possible if you have pushd and popd but due to the nature of
the '.', I think it is unlikely.
-- 
Eric M. Boehm
EMB978 at ALBNYVMS.BITNET
EMB978 at LEAH.ALBANY.EDU



More information about the Comp.unix.questions mailing list