[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bash programming question



Michael Meskes <meskes@debian.org> writes:

[snip]

> However, this does not work if there are blanks in the filename as $file
> would be incomplete. I cannot simply use -exec for find either since I call
> a function from the same script inside the loop. Finally I need to read some
> input during this function, so simply piping the find results and reading
> them via read doesn't work either. 

If you don't expect any files to have newline characters, you could
change the definition of $IFS to what normal find produces.  e.g.

    variable="`find . -print`"

    IFS='
    '        # No trailing spaces on the previous line.
    for file in $variable
    do
        somestuff "$file"
    done

You could so some strange stuff with file descriptors to let you read
from `find ... |' and stdin.  Sorry, no example, I haven't had enough
sleep this week to work it out.

If you can use zsh, you could do:

    for file in **/*; do somestuff "$file"; done

which will work even when some files contain a newline character.

You could put the find in a separate script:

    find . -print0 | xargs -r0 somestuff.sh

and in somestuff.sh:

    somestuff () { echo "$1" }

    for file in "$@"
    do
        somestuff "$file"
    done

You could rewrite it in Python and use walk() from os.path, or in Perl
and use File::Find.

-- 
	 Carey Evans  http://home.clear.net.nz/pages/c.evans/
"The risk of U.S. national security resting in the hands of adults who play
with children's toys during office hours is left as an exercise to the reader."
                                                       - Bruce Martin in RISKS


Reply to: