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


Groups > alt.os.linux.debian > #8431

Re: Simple script fools me?

From Ivan Shmakov <ivan@siamics.net>
Newsgroups comp.unix.shell, alt.os.linux.debian
Subject Re: Simple script fools me?
Date 2016-10-04 17:50 +0000
Organization Aioe.org NNTP Server
Message-ID <87mvikxaue.fsf@violet.siamics.net> (permalink)
References <nsivjc$ckm$1@dont-email.me>

Cross-posted to 2 groups.

Show all headers | View raw


>>>>> 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

Back to alt.os.linux.debian | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Simple script fools me? dog@gmail.com - 2016-09-29 11:57 +0000
  Re: Simple script fools me? William Unruh <unruh@invalid.ca> - 2016-09-29 14:04 +0000
    Re: Simple script fools me? Dan Purgert <dan@djph.net> - 2016-09-29 14:18 +0000
      Re: Simple script fools me? William Unruh <unruh@invalid.ca> - 2016-09-29 15:45 +0000
        Re: Simple script fools me? Dan Purgert <dan@djph.net> - 2016-09-29 18:16 +0000
  Re: Simple script fools me? Ivan Shmakov <ivan@siamics.net> - 2016-10-04 17:50 +0000

csiph-web