Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uwm.edu!zaphod.mps.ohio-state.edu!think.com!mintaka!bloom-beacon!deccrl!news.crl.dec.com!decvax.DEC.COM!evans From: evans@decvax.DEC.COM (Marc Evans) Newsgroups: comp.lang.perl Subject: Re: xargs in perl? Message-ID: <527@decvax.decvax.dec.com.UUCP> Date: 28 Jan 91 22:16:15 GMT References: Sender: news@decvax.dec.com.UUCP Reply-To: evans@decvax.DEC.COM Organization: Synergytics Lines: 75 In article , brister@decwrl.dec.com (James Brister) writes: |> Has anyone written a version of xargs in perl? Our binary versio is broken, |> and before I frustrate myself by trying to write it in perl, I thought I |> check here. Well, here is an implementation which isn't quite up to X/Open specs, but it should be close enough for most peoples needs. Note the magic number 50 for $maxarg. You can override this with the -n option, but you may want to adjust the default value for use on your system. Also note that the -i option isn't quite the same as X/Open defines, in that it doesn't take the optional argument (I'm just being lazy and using Getopts). - Marc ========8<========8<========cut=here========8<========8<======== #!/usr/bin/perl -P # Catch for sh/csh on systems without #! ability eval '(exit $?0)' && eval 'exec /usr/dec/perl -P -S $0 ${1+"$@"}"' & eval 'exec /usr/dec/perl -P -S $0 $argv:q' if 0; #include "getopts.pl" $program = $0; $program =~ s?.*/??; &Getopts('tpie:n:'); $shell = defined($ENV{'SHELL'}) ? $ENV{'SHELL'} : '/bin/sh'; $cmd = ($#ARGV >= 0) ? "@ARGV" : '/bin/echo'; $trace = defined($opt_t) ? 1 : 0; $prompt = defined($opt_p) ? 1 : 0; $insert = defined($opt_i) ? 1 : 0; $eof = defined($opt_e) ? $opt_e : '_'; $maxarg = defined($opt_n) ? $opt_n : 50; open(IN,") { chop; last if (/^$eof$/); next if (/^\s*$/ || ! &doprompt($_,$trace,$prompt)); if ($insert) { $line =~ s/{}/$_/g; } else { $line .= " $_"; } if ($insert || $nline == $maxarg) { print PIPE "$line\n"; $nline = 0; $line = "$cmd"; } $nline++; } print PIPE "$line\n" if ($nline > 1); close(PIPE); close(IN); exit(0); sub doprompt { local($Line,$Trace,$Prompt) = @_; local($Rval,$Ans) = (1,'n'); print STDOUT ($Prompt) ? "$Line ? " : "$Line\n" if ($Trace || $Prompt); if ($Prompt) { chop($Ans=); $Rval = ($Ans =~ /^\s*[Yy]/) ? 1 : 0; } return($Rval); } -- =========================================================================== Marc Evans - WB1GRH - evans@decvax.DEC.COM | Synergytics (603)635-8876 Unix and X Software Contractor | 21 Hinds Ln, Pelham, NH 03076 ===========================================================================