On Tue, Aug 14, 2007 at 09:17:46AM -0700, Bob McGowan wrote: > Steven R. wrote: >> On Tue, Aug 07, 2007 at 04:03:05PM -0700, Andrew Sackville-West wrote: >>> So what's the right way to do this? I hacked one together the other >>> day: >>> >>> IFS=$'\t\n'; for i in `find . -iname \*m4a`; do faad... blah blah blah >>> >>> and I knew it was a hack because setting $IFS just seems >>> bad... possible unintended consquences, but it worked. >>> >> I have seen something like the following: >> find | while read FILE; >> do echo "$FILE" >> done > > I missed the original postings (on vacation;). But, regarding IFS, a > couple of notes: > > 1. In "The UNIX Programming Environment", Kernighan and Pike, this > variable is used without any caveats, in several of the example scripts. > > 2. In all cases, the idiom used is: > > ifs=$IFS > IFS='....' # whatever you need for your particular problem. > ... # your code > IFS=$ifs > ... # any other processing that needs to be done > > This guarantees that the original value, whatever it is, is restored and > used for "normal" processing.
I guess there's nothing really wrong with mucking about with IFS. It certainly made it easy to do what I needed to do, and I guess that's a good thing. The problem I see though is that its not clear what it does. Starting a script, or even a block within a script, by changing IFS is cryptic at best. I've always felt and been taught to avoid using side effects and I think this definitely falls into this category because it is not clear from reading the code that changing IFS does anything. ANd if you are reading through some code and pull out some looping structure to use for some other purpose and don't know to grab the changed IFS as well, then the code will behave in a different and possibly unpredictable manner. THere was a protracted thread a while back on dealing with spaces in file names and such... I really should go back and read it. A
signature.asc
Description: Digital signature