|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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/ |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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! |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
> 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/ |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On May 22, 11:55 am, "Guillaume Dargaud"
<use_the_form_on_my_contact_p...@www.gdargaud.ne t> wrote: > > export -f Test; > > I tried that 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 {}" \; |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
> #!/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/ |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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> |
|
![]() |
| Outils de la discussion | |
|
|