OK, just when you thought it was safe... <g>

I have glob_dot_filenames set so that files beginning with "." are included.
Here's one of the things I'm calling zap_word() from:

flist=*
for fname in $flist; do
        if [ -d "$fname" ]; then
                flist=`zap_word "$fname" "$flist"`
        fi
done
echo [flist]=[$flist]

(Actually, I have a faster routine than the above that works fine, but I'm
trying to understand why the above does|doesn't work depending on the
following...)

When zap_word() includes 'echo $2 ...', the above works fine (all
directories including '.' and '..' are removed from flist).  When I change
that to 'echo "$2" ...', the result is '[flist]=[]'.  Knowing that a .
matches any single character, this makes some sense ('echo * | sed "s/ . /
/' wipes out '*' in the first successful 'if' iteration).

What's driving me nuts is that when I try to echo the 'echo' command in the
function in order to _see_ what's happening, I get the same result with and
without the quotes around $2, i.e. within the function...

#echo test
echo 'echo '$2' | sed '"s/^/ / ; s/\$/ / ; s/ $1 / / ; s/^ // ; s/ \$//"
...and...
#echo test
echo 'echo '"$2"' | sed '"s/^/ / ; s/\$/ / ; s/ $1 / / ; s/^ // ; s/ \$//"

both produce identical results:

echo * | sed s/^/ / ; s/$/ / ; s/ . / / ; s/^ // ; s/ $//

Thus, I can't _see_ what's happening.

If I change the 'flist=*' line to 'flist=". .. the_rest_of_the_filelist"
(i.e the actual expanded file list), then both techiques work, and both
return...

echo . .. the_rest_of_the_filelist | sed s/^/ / ; s/$/ / ; s/ . / / ; s/^ //
; s/ $//

...with the above #echo test commands.  Again, I can't _see_ any difference
in what's going on.  But, this tells me that expansion of '*' _before_
calling the function is somehow key to what's going on.

Finally, if I set flist=`echo *` (forcing the expansion of '*' before
calling the function), the results are identical to the previous test
(actual expanded file list).

Bottom line... I understand what you're saying about '"$2"' vs. '$2', but I
can't see or figure out the difference between them in this '.' case.  On
the up side, the '\$' works fine. :)

As always, thanks for your help.

bd

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of Cameron Simpson
Sent: Sunday, January 07, 2001 6:39 PM
To: [EMAIL PROTECTED]
Cc: Brad Doster
Subject: Re: Whole word substitution with sed


On Sun, Jan 07, 2001 at 05:12:06PM -0800, Brad Doster <[EMAIL PROTECTED]>
wrote:
| zap_word() {
|         # Arguments:    "word to delete" "word list to delete from"
|         echo $2 | sed "s/^/ / ; s/$/ / ; s/ $1 / / ; s/^ // ; s/ $//"
| }
|
| In this case, double quotes are needed because of the $1 usage.  I was
| delighted when Matthew suggested the double quotes, as that got it working
| for me.  Even better though, is now I understand *why*. :)

Just for the good habit, recode that as:

zap_word() {
        # Arguments:    "word to delete" "word list to delete from"
        echo "$2" | sed "s/^/ / ; s/\$/ / ; s/ $1 / / ; s/^ // ; s/ \$//"
}

That is, make sure $2 is properly quoted. It'll work fine as is until
the day you say:

        zap_word "foo  bar"     # nb: there's 8 chars in that string

Also backslash the $s in your sed which _aren't_ parameter subs.

By adopting these habits you make you code more robust against the
unanticipated needs of the future.

Cheers,
--
Cameron Simpson, DoD#743        [EMAIL PROTECTED]    http://www.zip.com.au/~cs/

There are old climbers, and there are bold climbers; but there are no old
bold climbers.



_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to