Mailinglist Archive: opensuse (1264 mails)

< Previous Next >
Re: [opensuse] Quick question: how to call a script function from "find"?
On 06/13/2017 12:52 AM, Carlos E. R. wrote:
I have this script:

#!/bin/bash

function doall()
{
NOMBRE="$*"
echo $NOMBRE
}

find /home/cer/Fusion/Videos/ -type d -exec doall '{}' \;



But I get this error message:

find: ‘doall’: No such file or directory
find: ‘doall’: No such file or directory
find: ‘doall’: No such file or directory

This is because doall is a shell function defined in the outer shell
by default only.

It is not recognising that "doall" is a function in the same script; instead
it tries to call an external command.

How can I do this?

Well, you could export the function definition to child shell processes

$ export -f doall

and the find command would look like this:

find /home/cer/Fusion/Videos/ -type d -exec sh -c 'doall "{}"' \;

However, this would launch one shell process per file, and I don't
think that is what you would like to have.


(Yes, I did try looking 'man', did not seem to find whatever I need to find).

man bash - search for "export -f"

Of course, I can write it up in two scripts, but seems ugly.

Depends - whatever it is doing.
You could launch a shell, but define the shell function inside it:

find /home/cer/Fusion/Videos/ -type d -exec sh -c '\
doall() {
NOMBRE="$@"
echo "$NOMBRE"
}
doall "$@"
' '{}' +

Of course, quoting is a bit more tricky. ;-)
To set how it works, you could add "set -x" somewhere in the shell code.

For further improvements, we'd need to know what 'doall()' is really
doing - it seems you have stripped it down to something reproducible.

Have fun,
Berny


--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
To contact the owner, e-mail: opensuse+owner@xxxxxxxxxxxx

< Previous Next >
Follow Ups
References