Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!sun-barr!cs.utexas.edu!csd4.milw.wisc.edu!leah!emb978 From: emb978@leah.Albany.Edu (Eric M. Boehm) Newsgroups: comp.unix.questions Subject: Re: Recursive shell scripts Keywords: Recursive Bourne '.' Message-ID: <1893@leah.Albany.Edu> Date: 8 Jul 89 21:05:11 GMT Distribution: usa Organization: The University at Albany, Computer Services Center Lines: 50 In article <343@dftsrv.gsfc.nasa.gov> setzer@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@ALBNYVMS.BITNET EMB978@LEAH.ALBANY.EDU