Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.unix.shell > #14025 > unrolled thread

Re: Simple script fools me?

Started byIvan Shmakov <ivan@siamics.net>
First post2016-10-04 17:50 +0000
Last post2016-10-04 17:50 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.unix.shell

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Simple script fools me? Ivan Shmakov <ivan@siamics.net> - 2016-10-04 17:50 +0000

#14025 — Re: Simple script fools me?

FromIvan Shmakov <ivan@siamics.net>
Date2016-10-04 17:50 +0000
SubjectRe: Simple script fools me?
Message-ID<87mvikxaue.fsf@violet.siamics.net>
>>>>> dog@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

[toc] | [standalone]


Back to top | Article view | comp.unix.shell


csiph-web