Google 网上论坛不再支持新的 Usenet 帖子或订阅项。历史内容仍可供查看。

Re: Simple script fools me?

已查看 14 次
跳至第一个未读帖子

Ivan Shmakov

未读,
2016年10月4日 13:50:232016/10/4
收件人
>>>>> 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 个新帖子