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

Re: bash - executing function in "find" command

%% Dexter <dexter2@nextra.sk> writes:

  >> find . | while read file; do <do something with $file>; done

  d> I tried before something like: 

  d> F=$(find .);for I in $F;do <do something with $I>; done

Besides breaking on filenames with embedded whitespace, this method has
the other major disadvantage that it collects the entire output into the
shell's memory and assigns it to a variable.  If the number of files is
very large, you'll be using a lot of memory in the shell to hold it all.
If the variable is exported, then you can run out of environment space
and you'll not be able to exec() any more programs (probably this
variable isn't exported, but...)

The pipe-to-while method has the pleasant property of never needing to
hold all the files at once: they are processed one at a time as they are
generated, then thrown away.

The disadvantage of the pipe-to-while method is that each element in the
pipeline is run in a subshell, so variables set inside the while loop
(for example) won't be set after the loop is complete[*].

[*] Some Bourne shells, like ksh and zsh, run the final command in the
    pipeline in the current shell.  But, bash and sh and others don't do
    that and POSIX doesn't require it, so relying on it is non-portable.

 Paul D. Smith <psmith@nortel.com>           HASMAT--HA Software Mthds & Tools
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist
   These are my opinions---Nortel Networks takes no responsibility for them.

Reply to: