News for hackers. Stuff that matters.
Re:Shelling Out Sucks (2012) by mzs on Tuesday May 12, 2015 @01:54PM (#9533585)
It's also wrong irregardless of the shell escaping. That will not always count the number of lines in all the files under a directory that contain at least one "foo" - when shelling-out, you have to be sure to use the tools correctly:

  $ mkdir sh
  $ cd sh
  $ >foo find . -type f -print0
  $ >bar head -1 foo foo
  $ # four lines with "foo" in file bar
  $ >foo echo foo
  $ mv foo 'bar
  > baz'
  $ # one line with "foo" in file bar?baz
  $ find . -type f -print0 | xargs -0 grep foo | wc -l
         3
  $ # Wait a minute that's not five, I KNOW there five lines!
  $ find . -type f -print0 | xargs -0 grep foo
  Binary file ./bar matches
  ./bar
  baz:foo
  $ # oh...
  $ find . -type f -exec grep -c foo '{}' \;
  4
  1
  $ # might as well get everything closer to correct
  $ env -i /usr/bin/find . -type f -execdir /usr/bin/grep -c foo -- '{}' \; | \
    env -i /usr/bin/awk '{t+=$1}END{print t}'
  5

There are no comments on this comment yet.