Re: Executing Shell Pipelines with ?find? _-exec?

Liste des GroupesRevenir à cu shell 
Sujet : Re: Executing Shell Pipelines with ?find? _-exec?
De : heller (at) *nospam* deepsoft.com (Robert Heller)
Groupes : comp.unix.shell comp.os.linux.misc
Date : 27. Apr 2024, 20:16:55
Autres entêtes
Organisation : Deepwoods Software
Message-ID : <WcWcnTF4lPG60rD7nZ2dnZfqnPidnZ2d@giganews.com>
References : 1 2
User-Agent : TkNews 3.0 (1.2.17)
At Sat, 27 Apr 2024 18:13:25 -0000 (UTC) Kaz Kylheku <643-408-1753@kylheku.com> wrote:

 
On 2024-04-27, Robert Heller <heller@deepsoft.com> wrote:
At Sat, 27 Apr 2024 13:40:52 -0000 (UTC) Christian Weisgerber <naddy@mips.inka.de> wrote:
A few days ago, Helmut Waitzmann pointed out a better solution over
on the German group:
 
  find . -name \*.blend -exec sh -c \
    '[ $(blendfile_version "$1" | jq -r .version ) \> 304 ]' sh {} \; \
    -print
 
You pass the filename as a positional parameter and reference it
as such in the command string.
>
Another option: find ... -print0 | xargs -0 -n 1 ...
 
And what is the ... after xargs? The goal of the command is
to print only those files F that individually satisfy the predicate
"[ $(blendfile_version F | jq -r .version) -gt 304 ]".
 
xargs will turn groups of files into command arguments, so
now you have to iterate over them; something like:
 
  .. | xargs sh 'for x in "$@"; do\
                   [ $(blendfile_version "$x" | \
                       jq -r .version ) -gt 304 ] && printf "%s\n" "$x"
                 done'
 
It doesn't seem like an improvement. It does at least as much work
(still has to dispatch blendfile_version as many times as there
are files).  It's more verbose, and uses GNU extensions.

The '-n 1' tells xargs to run the command given to xargs with only one file at
a time.  There is no need for the "for x in "$@"; do...".

find . -name '*.blend' -print0 | xargs -0 -n 1 'sh [ $(blendfile_version "$1" | \
    jq -r .version ) -gt 304 ] && printf "%s\n" "$1"'


The combination of find's -print0 and xargs -0 is to pass the names as nul
terminated strings rather then newline separated strings. This avoids problems
with file names containing characters that the shell with try to make sense of
(eg spaces, etc.).

 
If extensions are allowed, we can just drop find entirely and use
use double star globbing in Bash or any other shell that has it:
 
  for x in **/*.blend; do
    [ $(blendfile_version "$x" | jq -r .version ) -gt 304 ] && printf "%s\n" "$x"
  done
 
 

--
Robert Heller             -- Cell: 413-658-7953 GV: 978-633-5364
Deepwoods Software        -- Custom Software Services
http://www.deepsoft.com/  -- Linux Administration Services
heller@deepsoft.com       -- Webhosting Services
                                                                                                                    

Date Sujet#  Auteur
27 Apr 24 * Executing Shell Pipelines with “find -exec”8Lawrence D'Oliveiro
27 Apr 24 +* Re: Executing Shell Pipelines with “find -exec”6Christian Weisgerber
27 Apr 24 i+* Re: Executing Shell Pipelines with ?find? _-exec?4Robert Heller
27 Apr 24 ii`* Re: Executing Shell Pipelines with ?find? _-exec?3Kaz Kylheku
27 Apr 24 ii `* Re: Executing Shell Pipelines with ?find? _-exec?2Robert Heller
5 May 24 ii  `- Re: Executing Shell Pipelines with ?find? _-exec?1Friedhelm Waitzmann
28 Apr 24 i`- Re: Executing Shell Pipelines with “find -exec”1Lawrence D'Oliveiro
5 May 24 `- Re: Executing Shell Pipelines with “find -exec”1Friedhelm Waitzmann

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal