Re: Learning Bash: recursion

From: Chris F.A. Johnson (c.fa.johnson_at_rogers.com)
Date: 10/10/03


Date: 10 Oct 2003 14:10:22 GMT

On Fri, 10 Oct 2003 at 04:27 GMT, Sak wrote:
> Thanks for responding Chris. I have another curiosity, not a major issue,
> and some follow up to your post inline...
>
> Chris F.A. Johnson wrote:
[snip]
>> If you want a slash after the name of a directory, you have to
>> put it there.
>
>> recdir () {
>> tab=$tab$singletab
>>
>> for file in "$@"; do
>> thisfile=$thisfile/$file
>>
>> if [ -d "$thisfile" ]; then
>> echo -e $tab$file/
>> recdir $(command ls $thisfile)
>> else
>> echo -e $tab$file
>> fi
>>
>> thisfile=${thisfile%/*}
>> done
>>
>> tab=${tab%"\t"}
>> }
>
> This works great. But here's where my next question comes in. When I
> originally tried it, I tried this...
>
> for file in "$@"; do
>
> if [ -d $file ]; then
> echo -e $tab$file/
> else
> echo -e $tab$file
> fi
>
> thisfile=$thisfile/$file
>
> if [ -d "$thisfile" ]; then
> recdir $(command ls $thisfile)
> fi
>
> ...it didn't change the listing whatsoever. What has me confused is that in
> the original version, echo -e $tab$file precedes thisfile=$thisfile/$file
> and works just fine in producing the listing. However, when I tried to
> include the if statement to differentiate between a directory and file, it
> had no effect. Granted, your version is much more elegant; after-all why
> create an additional if statement when there's already one there that can
> be used for this. I didn't think of that. But what has me curious is why
> it only produces the forward slash after thisfile=$thisfile/$file ?

     What does the variable "$file" contain? It's the name of a
     directory without the path to it, so unless there's a directory
     with that name in the current directory, the -d test will
     fail. (And, of course, can give falsely identify a file as a
     directory if that exists in $PWD.)

     The path to the directory is in "$thisfile"; once you put the two
     together, it can find the directory, and "-d" will succeed.

-- 
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License


Relevant Pages

  • Football for Baseball
    ... 193 Chris Simms ... 199 Rudi Johnson ... 225 Mike Peterson ... Marvin Harrison ...
    (rec.collecting.sport.football)
  • football for baseball
    ... Thing is we only collect cards of a few baseball players ... 193 Chris Simms ... 199 Rudi Johnson ... Marvin Harrison ...
    (rec.collecting.sport.baseball)
  • Re: sed problem: variables countaining special symbols
    ... Chris F.A. Johnson wrote: ... My script is falling over with a sed problem. ... That's bacause $NPASS contains a slash. ...
    (comp.unix.shell)
  • Re: Interesting list
    ... On 2007-06-13, Chris F.A. Johnson wrote: ... of the top ten ways to irritate your Web site's visitors. ... Pop Ups ...
    (alt.html)
  • Re: help w/unix loops
    ... >> Is it the last two characters preceding the space? ... Read the man page for your shell to learn how to do it. ... My code in this post is copyright 2003, Chris F.A. Johnson ...
    (comp.unix.shell)