|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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/> |
|
![]() |
| Outils de la discussion | |
|
|