From ea089f538ee7e55a3059341901daac8f11eb94c1 Mon Sep 17 00:00:00 2001 From: graehl Date: Mon, 28 Jun 2010 18:35:54 +0000 Subject: shell escape git-svn-id: https://ws10smt.googlecode.com/svn/trunk@39 ec762483-ff6d-05da-a07a-a48fb63a330f --- vest/parallelize.pl | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/vest/parallelize.pl b/vest/parallelize.pl index 6a2da1bb..28260155 100755 --- a/vest/parallelize.pl +++ b/vest/parallelize.pl @@ -35,6 +35,7 @@ my $randp=300; my $tryp=50; my $no_which; my $no_cd; + my $DEBUG=$ENV{DEBUG}; sub debug { if ($DEBUG) { @@ -48,6 +49,17 @@ sub abspath($) { chomp $a; $a } +my $is_shell_special=qr.[ \t\n\\><|&;"'`~*?{}$!()].; +my $shell_escape_in_quote=qr.[\\"\$`!].; +sub escape_shell { + my ($arg)=@_; + return undef unless defined $arg; + if ($arg =~ /$is_shell_special/) { + $arg =~ s/($shell_escape_in_quote)/\\$1/g; + return "\"$arg\""; + } + return $arg; +} my $abscwd=abspath(&getcwd); sub print_help; @@ -81,12 +93,8 @@ if ($no_which) { die "$prog not found - $cmd" unless $cmd; } #$cmd=abspath($cmd); -for my $arg (@ARGV){ - if ($arg=~ /\s/){ - $cmd .= " \"$arg\""; - } else { - $cmd .= " $arg" - } +for my $arg (@ARGV) { + $cmd .= " ".shell_escape($arg); } die "Please specify a command to parallelize\n" if $cmd eq ''; -- cgit v1.2.3