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 > Multiple shell commands using find's -exec
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Multiple shell commands using find's -exec

Réponse
 
LinkBack Outils de la discussion
Vieux 13/03/2008, 18h27   #1
Jeenu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Multiple shell commands using find's -exec

Hi,

I'm just wondering whether there is a way of executing multiple shell
commands with find's -exec switch? If either of ";", "&&" or "|" is
used in between commands, then shell will interpret it as two separate
shell commands and some times fail. I can't even use \; since find
interprets it as the end of it's arguments. Any other go, than putting
all commands into a script?

Thanks
Jeenu
  Réponse avec citation
Vieux 13/03/2008, 18h32   #2
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple shell commands using find's -exec

2008-03-13, 10:27(-07), Jeenu:
> Hi,
>
> I'm just wondering whether there is a way of executing multiple shell
> commands with find's -exec switch? If either of ";", "&&" or "|" is
> used in between commands, then shell will interpret it as two separate
> shell commands and some times fail. I can't even use \; since find
> interprets it as the end of it's arguments. Any other go, than putting
> all commands into a script?

[...]

find ... -exec cmd1 {} \; -exec cmd2 {} \;

will execute cmd2 if cmd1 exit status is 0 with one filename as
argument.

find ... -exec cmd1 {} + -exec cmd2 {} +

will run cmd1 and cmd2 sequentially with as many file names as
possible at a time.

For more complicated stuff, call sh:

find ... -exec sh -c '
file=$1
cmd1 "$1" | cmd2 "$1"' {} {} \;

--
Stéphane
  Réponse avec citation
Vieux 13/03/2008, 18h35   #3
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple shell commands using find's -exec

2008-03-13, 18:39(+01), pk:
> Jeenu wrote:

[...]
>> I'm just wondering whether there is a way of executing multiple shell
>> commands with find's -exec switch? If either of ";", "&&" or "|" is
>> used in between commands, then shell will interpret it as two separate
>> shell commands and some times fail. I can't even use \; since find
>> interprets it as the end of it's arguments. Any other go, than putting
>> all commands into a script?

[...]
> $ find -exec sh -c "wc -l '{}'; head -n 2 '{}' && tail -n 2 '{}'" \;

[...]

That's non-standard and dangerous. Standardly, you can only
expect {} to be expanded to the filename if it consistutes an
argument of find by itself.

find ... -exec ... {} \; # OK
find ... -exec ... x{} \; # NOK

find ... -exec sh -c '... "x$1"' {} {} \; # OK

--
Stéphane
  Réponse avec citation
Vieux 13/03/2008, 18h39   #4
pk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple shell commands using find's -exec

Jeenu wrote:

> Hi,
>
> I'm just wondering whether there is a way of executing multiple shell
> commands with find's -exec switch? If either of ";", "&&" or "|" is
> used in between commands, then shell will interpret it as two separate
> shell commands and some times fail. I can't even use \; since find
> interprets it as the end of it's arguments. Any other go, than putting
> all commands into a script?


$ ls
README
$ find -exec sh -c "wc -l '{}'; head -n 2 '{}' && tail -n 2 '{}'" \;
61 ./README
Description
-------------
src Source files of the program.
testsuite Check correct function of the program.


  Réponse avec citation
Vieux 13/03/2008, 18h43   #5
Jeenu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple shell commands using find's -exec

On Mar 13, 10:32 pm, Stephane CHAZELAS <this.addr...@is.invalid>
wrote:
> 2008-03-13, 10:27(-07), Jeenu:> Hi,
>
> > I'm just wondering whether there is a way of executing multiple shell
> > commands with find's -exec switch? If either of ";", "&&" or "|" is
> > used in between commands, then shell will interpret it as two separate
> > shell commands and some times fail. I can't even use \; since find
> > interprets it as the end of it's arguments. Any other go, than putting
> > all commands into a script?

>
> [...]
>
> find ... -exec cmd1 {} \; -exec cmd2 {} \;
>
> will execute cmd2 if cmd1 exit status is 0 with one filename as
> argument.
>
> find ... -exec cmd1 {} + -exec cmd2 {} +
>
> will run cmd1 and cmd2 sequentially with as many file names as
> possible at a time.
>
> For more complicated stuff, call sh:
>
> find ... -exec sh -c '
> file=$1
> cmd1 "$1" | cmd2 "$1"' {} {} \;
>
> --
> Stéphane


Stéphane, can you explain the "+ -exec ... +" thing above?
  Réponse avec citation
Vieux 13/03/2008, 18h50   #6
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple shell commands using find's -exec

2008-03-13, 10:43(-07), Jeenu:
[...]
>> find ... -exec cmd1 {} \; -exec cmd2 {} \;

[...]
>> find ... -exec cmd1 {} + -exec cmd2 {} +

[...]
> Stéphane, can you explain the "+ -exec ... +" thing above?


+ is an alternative to ; to terminate a -exec option.

Only valid when following a {}, it's meant to be used as a more
reliable alternative to xargs. See your find man page or the
spec:
http://opengroup.org/onlinepubs/0096...ties/find.html
for more details.

--
Stéphane
  Réponse avec citation
Vieux 13/03/2008, 18h55   #7
pk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple shell commands using find's -exec

Stephane CHAZELAS wrote:

>> $ find -exec sh -c "wc -l '{}'; head -n 2 '{}' && tail -n 2 '{}'" \;

> [...]
>
> That's non-standard and dangerous. Standardly, you can only
> expect {} to be expanded to the filename if it consistutes an
> argument of find by itself.
>
> find ... -exec ... {} \; # OK
> find ... -exec ... x{} \; # NOK
>
> find ... -exec sh -c '... "x$1"' {} {} \; # OK
>


Yes, I should have specified GNU find.
Thanks for the correction.
Unfortunately, I can only access linux systems with GNU utilities so my
solutions are always based on what I can use. I'll probably use a signature
to indicate that.

Thanks

  Réponse avec citation
Vieux 13/03/2008, 18h56   #8
Jeenu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple shell commands using find's -exec

On Mar 13, 10:50 pm, Stephane CHAZELAS <this.addr...@is.invalid>
wrote:
> 2008-03-13, 10:43(-07), Jeenu:
> [...]
>
> >> find ... -exec cmd1 {} \; -exec cmd2 {} \;

> [...]
> >> find ... -exec cmd1 {} + -exec cmd2 {} +

> [...]
> > Stéphane, can you explain the "+ -exec ... +" thing above?

>
> + is an alternative to ; to terminate a -exec option.
>
> Only valid when following a {}, it's meant to be used as a more
> reliable alternative to xargs. See your find man page or the
> spec:http://opengroup.org/onlinepubs/0096...ties/find.html
> for more details.
>
> --
> Stéphane


Thanks you Stéphane, pk
  Réponse avec citation
Vieux 14/03/2008, 11h48   #9
Sven Mascheck
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple shell commands using find's -exec

Stephane CHAZELAS wrote:

> find ... -exec ... {} \; # OK
> find ... -exec ... x{} \; # NOK


I had been curious upon an earlier posting of yours
and had looked at some implementations:

The traditional find requires {} to be a separate argument. POSIX hasn't
changed this. The new behaviour was introduced around 386BSD, according
to the FreeBSD manpage archive (haven't nailed it down better).

These variants for example accept an embedded {}:
· all traditional and free BSDs, BSDi/OS
· GNU (probably from the beginning)
· AST (probably from the beginning)
· busybox since 1.1.0-pre1, 20051003 (where -exec was implemented itself)
· OpenServer 6.0.0 (bin and posix)

but these don't accept it:
· SunOS 5.9
· IRIX 6.5.22
· HP-UX 11.11 / 11.31
· sfind-1.1
· OpenServer 6.0.0 (u95-find)
· EP/IX 2.2.1
--
<http://www.in-ulm.de/~mascheck/various/find/>
  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 05h11.


É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,14335 seconds with 17 queries