[plug] grep + xargs = missing separator?

Russell Steicke r.steicke at bom.gov.au
Sat Sep 11 13:20:57 WST 2004


On Sat, Sep 11, 2004 at 12:17:09PM +0800, Andrew Furey wrote:
...
> Yes, on further investigation that seems to be it. It seems the
> easiest way to fix it would be to run "echo --" before (or after) the
> grep process, within the xargs call, something like
> 
> # find /var/spool/exim/input -name \* | xargs echo --; grep -n -i -2
> --binary-files=text "badmailfrom"
> 
> Except that this doesn't work because xargs only deals with the echo
> command, then the grep runs on its own.
> 
> Is there any way to get around this, maybe some xargs parameter (I
> don't see anything)? I could always use --max-chars (or whichever
> limit is being hit) but I'd rather it scaled better.

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.

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



-- 
Russell Steicke

-- Fortune says:
If I have to lay an egg for my country, I'll do it.
		-- Bob Hope



More information about the plug mailing list