Re: Learning Bash: recursion
From: Chris F.A. Johnson (c.fa.johnson_at_rogers.com)
Date: 10/10/03
- Next message: Chris F.A. Johnson: "Re: Read command and trailing spaces"
- Previous message: Dan Mercer: "Re: dev zero into dd varies in length"
- In reply to: Sak: "Re: Learning Bash: recursion"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Chris F.A. Johnson: "Re: Read command and trailing spaces"
- Previous message: Dan Mercer: "Re: dev zero into dd varies in length"
- In reply to: Sak: "Re: Learning Bash: recursion"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|