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

Re: bash - executing function in "find" command

%% "Chris F.A. Johnson" <cfajohnson@gmail.com> writes:

  cfaj> On 2005-07-06, Paul Smith wrote:

  >> 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[*].

  cfaj> find . | {
  cfaj>   while read line
  cfaj>   do
  cfaj>      : whatever
  cfaj>      word=${line%% *}
  cfaj>   done

  cfaj>   : variables set within the loop are still available here, e.g.:
  cfaj>   printf "%s\n" "$word"

  cfaj> }

  cfaj> : but not here

Good point; sometimes this is good enough.  Not always, though--unless
you're willing to have the entire remainder of your script to appear in
the pipeline block :-).

A very handy technique to have in the toolbox though.


 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: