find | while read

Byron Clark byron at theclarkfamily.name
Sat Apr 12 11:43:49 MDT 2008


On Sat, Apr 12, 2008 at 09:52:47AM -0600, Stuart Jansen wrote:
> Ever since I was taught "while read" as a method to avoid whitespace
> problems, it has been one of my favorite shell scripting tricks.
> Unfortunately, while working on a git script yesterday, I discovered an
> instance where it doesn't work. Specifically, Bash is leaking the output
> of find to the screen and horking stdin.
> 
> I found a workaround, but it's more complex than I like. I've included
> three generations of a simplified script as an example. What I'd like to
> know is, does anyone know a more sane method for looping over a list of
> files produced by a command like find, git or anything else?
> 
> NOTE: To save time, let me point out that git doesn't have an equivalent
> of find's --exec, so that's out. In addition, the script has to handle
> whitespace in filenames so "for FILE in $( ... )" is also out. I have a
> sneaking suspicion that changing $IFS could lead to a solution, but that
> just freaks me out.

Yep, changing IFS gives a very simple solution:

OLDIFS=$IFS
# the single quote on the next line is followed by a newline
IFS='
'
for FILE in $(find . -name \*.txt); do
    echo Spell checking $FILE
    aspell -c "$FILE"
done
IFS=$OLDIFS

-- 
Byron Clark
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://plug.org/pipermail/plug/attachments/20080412/0c0af9d4/attachment.bin 


More information about the PLUG mailing list