[plug] grep + xargs = missing separator?

Andrew Furey Andrew.Furey at gmail.com
Mon Sep 13 19:50:49 WST 2004


Russell Steicke wrote:
> This should work:
> 
>   find /var/spool/exim/input -type f -print0 | \
>     xargs -0 sh -c \
>       'echo -- ; grep -n -i -2 --binary-files=text "badmailfrom" "$@"' foo
> 
> ``sh -c string'' runs a shell with the string (in single quotes above)
> as command input.  The ``foo'' on the end is important: it gets
> assigned to $0 by the shell, which isn't included in the "$@"
> parameter expansion.  Without this you'd miss the first file on every
> grep invocation, because sh wouldn't pass it to grep.

Cool, that seems to do the job nicely.

> I've changed a couple of other things: ``-name *'' is the default, you
> don't need it.

Of course, silly me; it was probably left over from earlier
wildcard-limiting activities.

> ``-print0'' is good practice if you have it available
> (GNU grep and the open source BSDs have it) to null-separate rather
> than newline-separate the filenames, so that filenames with odd
> characters are handled properly.  With that you need the matching
> ``-0'' flag to xargs, so it looks for filenames separated by null
> rather than separated by white space.

Hm, have to remember that one...



James Devenish wrote:
> Is there a specific reason you aren't just using `grep -r`???
> # grep -r -n -i -2 --binary-files=text "badmailfrom" /var/spool/exim/input

The files are all in the one directory, so I don't need it to be
recursive; but considering that there are two files per message for
exim, I hit the shell limits ("bash: /bin/grep: Argument list too
long") too often to be useful, which is why I switched to xargs in the
first place :)

-- 
Linux supports the notion of a command line or a shell for the same
reason that only children read books with only pictures in them.
Language, be it English or something else, is the only tool flexible
enough to accomplish a sufficiently broad range of tasks.
                          -- Bill Garrett



More information about the plug mailing list