Mailinglist Archive: opensuse (1264 mails)

< Previous Next >
Re: [opensuse] Quick question: how to call a script function from "find"?
On 2017-06-13 02:01, Bernhard Voelker wrote:
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.

Huh.

I've never had to do anything like that in any script. It is "find" which is
special.


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

man bash - search for "export -f"

Well, of course, "man find" would say nothing about that...



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.

No, I haven't. doall is not written yet. I go by steps. The script only has
what you see.


The intention was to convert:

find /home/cer/Fusion/Videos/ -type d -exec sudo chown cer:cer '{}' \;
find /home/cer/Fusion/Videos/ -type d -exec chmod u+r+w+x,g+w+x,o+r-w-x,+t '{}'
\;
find /home/cer/Fusion/Videos/ -type d -exec sudo chown cer-g:cer '{}' \;
#setfacl -m u:wwwrun:rx /data/waterhoard/Fusion.uno/Videos

to a single "find". And calling a function in the same script seemed to me the
clearest way of doing it.


--
Cheers / Saludos,

Carlos E. R.
(from 42.2 x86_64 "Malachite" at Telcontar)

< Previous Next >