Google Groupes n'accepte plus les nouveaux posts ni abonnements Usenet. Les contenus de l'historique resteront visibles.

Re: Simple script fools me?

14 vues
Accéder directement au premier message non lu

Ivan Shmakov

non lue,
4 oct. 2016, 13:50:2304/10/2016
à
>>>>> d...@gmail.com writes:

[Cross-posting to comp.unix.shell as I see nothing
Debian-specific here.]

> Simple script fools me?

> #!/bin/bash
> echo -n " find files here containing PHRASE: "
> echo $1
> find . -type f | \
> grep -v pdf | \
> grep -v wav | \
> grep -v mp3 | \
> xargs grep -l "$1" | grep -v grep
> exit.

> I want to avoid the output-lines:

> grep: ./JS/Documents/Scratch: No such file or directory
> grep: Projects/Circle30.sb: No such file or directory
> grep: ./JS/Documents/Scratch: No such file or directory
> grep: Projects/ZeroOne.sb: No such file or directory

find . -type f \
-not -name \*pdf\* \
-not -name \*wav\* \
-not -name \*mp3\* \
-exec grep -l -- "$1" {} +

Somehow, I assume that the 'grep -v grep' part was left from a
'ps ax | grep something' command, so I've omitted it here.

> Not only what-to-use, but why mine-fails.

Because of whitespace.

Suppose that find(1) gives us these filenames:

To sleep: perchance to dream: ay, there's the rub.wav
For in that sleep of death what dreams may come.pdf
When we have shuffled off this mortal coil.mp3
Must give us pause: there's the respect.jpeg
That makes calamity of so long life.text

After the three-grep(1) sequence, what remains is:

Must give us pause: there's the respect.jpeg
That makes calamity of so long life.text

As long as xargs(1) is concerned, these two lines contain the
following four parameters to be passed to the 'grep -l "$1"'
command:

Must
give
us
pause:

And just after these four, xargs(1) stumbles upon an unmatched
single quote in "there's", but that's beside the point.

The usual solution is to put as much logic into the find(1)
command, and use '-exec COMMAND ARGUMENTS... -- {} +' at the end
of said find(1) invocation. Alternatively, it's possible to use
find -print0, grep -z, and xargs -r0, in which case only the
ASCII "NUL" control code (never possible in Unix filenames) will
be treated as separator.

It's of course possible to do even more complex stuff with
find(1). Consider, e. g.:

$ find \( -type f -name \*.pdf -exec pdfsearch -- foo {} + \) \
-or \( -type f -name \*.jpeg -exec exifsearch -- bar {} + \)

--
FSF associate member #7257 58F8 0F47 53F5 2EB2 F6A5 8916 3013 B6A0 230E 334A
0 nouveau message