xargs in Perl (was Re: Look! An xargs!! (Re: recursive grep))

Randal Schwartz merlyn at iwarp.intel.com
Sat Sep 9 15:17:08 AEST 1989


In article <680 at lakart.UUCP>, dg at lakart (David Goodenough) writes:
| OK - will you all get off my case, already!!
[awk program deleted]

And, here it is in Perl (of course...)...
================================================== cut here
#!/usr/bin/perl

$lengthmax = 512;
@command = $#ARGV > -1 ? @ARGV : ("/bin/echo");
$lengthcommand = length(join(" ", @command)) + 1;
while (<stdin>) {
	if ($lengthcommand + length($args) + length($_) > $lengthmax) {
		system @command, split(/\n/, $args);
		$err = 1 if $?;
		$args = "";
	}
	$args .= $_;
}
if (length($args)) {
	system @command, split(/\n/, $args);
	$err = 1 if $?;
}
exit $err;
================================================== cut here too

This version handles commands with *any* special characters (provided
you had already quoted it for the shell properly), and arguments with
any characters *except* newline (gotta have *one* reserved character
:-).

| So there :-P  Like I said, I do it all with awk :-)

And, I do it all with Perl, now.

Just another Perl hacker,
-- 
/== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\
| on contract to Intel, Hillsboro, Oregon, USA                           |
| merlyn at iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/



More information about the Comp.unix.wizards mailing list