setzer@nssdcs (William Setzer (IDM)) (07/01/89)
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 ------------- According to what the man page says, this should work, but it doesn't. For some reason or another, the $I parameter isn't being passed back into the program (I always get the same value for $1). I know they can be passed; I wrote a script that did it. My best guess is that somehow it finds itself linked in memory and decided to link to the already running process. I tried putting parens around '. $0 $I' to run it in it's own subshell, but that didn't work either. I'm stumped. Can anyone help me? Thanx. Oh yeah, I'm running SunOS 4.0 (it's really BSD 4.?). William Setzer setzer@nssdcs.gsfc.nasa.gov
emb978@leah.Albany.Edu (Eric M. Boehm) (07/09/89)
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