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

Re: bash - executing function in "find" command



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

  d> But I want to use bash function instead of command.

You can't.

  d> Problem is not, that command "find" runs in different environment, and
  d> doesn't know variables and functions from parent shell.

Yes it is.

  d> Because i tried:
  d> A="XXX"
  d> $find . -exec echo $A \;
  d> XXX

That example is not testing the right thing, because $A is expanded by
your shell before find is invoked.  In order to see if it's really
working you'd have to escape it so that $A itself is passed to find,
like this:

    find . -exec echo '$A' \;
    $A
    $A
    $A

etc.

  d> And I also tried:
  d> export -f functionname(){
  d> .....
  d> }

  d> Somebody has a idea how to run function there?

You can't.

The problem is find -exec isn't running a shell at all; it's running
that command directly by using fork/exec.  Shell operations don't work
there.

Your only possibility of using a shell script is to run a shell as the
exec command, something like:

    find . -exec /bin/bash -c "your script here" \;

You'll need to check the bash man page to make sure that the function
definition is put into the right file so that it's read when the shell
starts this way (this is a non-interactive, non-login shell).


Far simpler is to just write a shell script instead of using a function,
or even use a different method of scripting; maybe something like:

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

-- 
-------------------------------------------------------------------------------
 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: