Unix Shell - Bash function in find command

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > May 2007 > Bash function in find command





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Bash function in find command
Guillaume Dargaud

2007-05-21, 1:19 pm

Hello all,
the following doesn't work:

function Test {
echo "Test $1"
}
find -maxdepth 1 -iname \*.jpg -exec Test {} \;

If Test is a file, it works.
Why is there a difference and how can I use a function with the find command
?

Thanks
--
Guillaume Dargaud
http://www.gdargaud.net/


Stephane CHAZELAS

2007-05-21, 1:19 pm

2007-05-21, 19:08(+02), Guillaume Dargaud:
> Hello all,
> the following doesn't work:
>
> function Test {
> echo "Test $1"
> }
> find -maxdepth 1 -iname \*.jpg -exec Test {} \;
>
> If Test is a file, it works.
> Why is there a difference and how can I use a function with the find command
> ?

[...]

A shell function is only known to the shell that defines it.
find has no knowledge of it.

find . -maxdepth 1 -iname \*.jpg -exec sh -c '
Test() {
printf "Test %s\n" "$1"
}
for i do
Test "$i"
done' sh-in-find {} +

But

for i in *.[jJ][pP][gG]; do
Test "$i"
done

should be enough.
(note that -maxdepth and -iname are not Unix, but are GNU).

--
Stéphane
Bill Marcum

2007-05-21, 1:19 pm

On Mon, 21 May 2007 19:08:26 +0200, Guillaume Dargaud
<USE_MY_WEB_FORM@gdargaud.net> wrote:
>
>
> Hello all,
> the following doesn't work:
>
> function Test {
> echo "Test $1"
> }
> find -maxdepth 1 -iname \*.jpg -exec Test {} \;
>
> If Test is a file, it works.
> Why is there a difference and how can I use a function with the find command
> ?
>

'find -exec' doesn't invoke a shell unless you tell it to.
-exec sh -c "script that defines and calls Test" {} \;


--
FORCE YOURSELF TO RELAX!
Martin Krischik

2007-05-22, 7:18 am

Guillaume Dargaud schrieb:
> Hello all,
> the following doesn't work:
>
> function Test {
> echo "Test $1"
> }
> find -maxdepth 1 -iname \*.jpg -exec Test {} \;
>
> If Test is a file, it works.
> Why is there a difference and how can I use a function with the find command
> ?


No, but you forgot:

export -f Test;

find starts a new shell and if you don't export the function it won't be
available.

Martin

--
Martin Krischik
Guillaume Dargaud

2007-05-22, 7:18 am

> export -f Test;
I tried that after the declaration and before the find, but it doesn't make
a difference...

As for using a 'for' loop, I was just looking for a more general solution,
thanks.
--
Guillaume Dargaud
http://www.gdargaud.net/


tmp123

2007-05-22, 1:25 pm

On May 22, 11:55 am, "Guillaume Dargaud"
<use_the_form_on_my_contact_p...@www.gdargaud.net> wrote:
>
> I tried t

hat after the declaration and before the find, but it doesn't make
> a difference...
>
> Guillaume Dargaudhttp://www.gdargaud.net/


The full script could be something like:

#!/bin/bash
#
function Test {
echo "Test $1"
}

export -f Test

find . -exec bash -c "Test {}" \;

Guillaume Dargaud

2007-05-22, 1:25 pm

> #!/bin/bash
> #
> function Test {
> echo "Test $1"
> }
>
> export -f Test
>
> find . -exec bash -c "Test {}" \;



Strange that's exactly how I first tried it and it didn't work at the time.
I your example and it runs fine... Scratching head...

Thanks
--
Guillaume Dargaud
http://www.gdargaud.net/


Geoff Clare

2007-05-24, 1:18 pm

tmp123 wrote:

> #!/bin/bash
> #
> function Test {
> echo "Test $1"
> }
>
> export -f Test
>
> find . -exec bash -c "Test {}" \;


Using {} as part of a find -exec argument is not portable. Where
supported, using it directly in a shell command is unsafe.

The portable and safe equivalent to the unsafe:

find . -exec sh -c 'somecommand {}' \;

is:

find . -exec sh -c 'somecommand "$1"' inline_sh {} \;

(unless you have a very old shell that assigns the arguments incorrectly,
in which case use "{} {}" instead of "inline_sh {}").

--
Geoff Clare <netnews@gclare.org.uk>
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com