PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Bash function in find command
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Bash function in find command

Réponse
 
LinkBack Outils de la discussion
Vieux 21/05/2007, 18h08   #1
Guillaume Dargaud
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Bash function in find command

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/


  Réponse avec citation
Vieux 21/05/2007, 18h21   #2
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bash function in find command

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
  Réponse avec citation
Vieux 21/05/2007, 18h59   #3
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bash function in find command

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!
  Réponse avec citation
Vieux 22/05/2007, 09h01   #4
Martin Krischik
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bash function in find command

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
  Réponse avec citation
Vieux 22/05/2007, 10h55   #5
Guillaume Dargaud
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bash function in find command

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


  Réponse avec citation
Vieux 22/05/2007, 13h41   #6
tmp123
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bash function in find command

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 {}" \;

  Réponse avec citation
Vieux 22/05/2007, 15h41   #7
Guillaume Dargaud
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bash function in find command

> #!/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/


  Réponse avec citation
Vieux 24/05/2007, 13h49   #8
Geoff Clare
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bash function in find command

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>
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h11.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14392 seconds with 16 queries