-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #248 from mknos/xargs-forever
xargs: infinite loop with -n
- Loading branch information
Showing
1 changed file
with
22 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,13 +17,19 @@ License: | |
# Gurusamy Sarathy <[email protected]> | ||
# | ||
|
||
use File::Basename qw(basename); | ||
use Getopt::Std; | ||
use Text::ParseWords; | ||
|
||
use constant EX_SUCCESS => 0; | ||
use constant EX_FAILURE => 1; | ||
|
||
my $Program = basename($0); | ||
|
||
my %o; | ||
getopts('tn:l:s:I:', \%o) or die <<USAGE; | ||
Usage: | ||
$0 [-t] [-n num] [-l num] [-s size] [-I repl] prog [args] | ||
$Program [-t] [-n num] [-l num] [-s size] [-I repl] prog [args] | ||
-t trace execution (prints commands to STDERR) | ||
-n num pass at most 'num' arguments in each invocation of 'prog' | ||
|
@@ -33,6 +39,18 @@ Usage: | |
before execution | ||
USAGE | ||
|
||
for my $opt (qw( l n s )) { | ||
next unless (defined $o{$opt}); | ||
if (!length($o{$opt}) || $o{$opt} =~ m/\D/) { | ||
warn "$Program: option $opt: invalid number '$o{$opt}'\n"; | ||
exit EX_FAILURE; | ||
} | ||
if ($o{$opt} == 0) { | ||
warn "$Program: option $opt: number must be > 0\n"; | ||
exit EX_FAILURE; | ||
} | ||
} | ||
|
||
my @args = (); | ||
|
||
$o{I} ||= '{}' if exists $o{I}; | ||
|
@@ -53,15 +71,15 @@ while (1) { | |
my @run = @ARGV; | ||
push @run, 'echo' unless (@run); | ||
if ($o{I}) { | ||
exit(0) unless length $line; | ||
exit(EX_SUCCESS) unless length $line; | ||
for (@run) { s/\Q$o{I}\E/$line/g; } | ||
} | ||
elsif ($o{n}) { | ||
exit(0) unless @args; | ||
exit(EX_SUCCESS) unless @args; | ||
push @run, splice(@args, 0, $o{n}); | ||
} | ||
else { | ||
exit(0) unless @args; | ||
exit(EX_SUCCESS) unless @args; | ||
push @run, @args; | ||
@args = (); | ||
} | ||
|