From c48c5fb7b95e8f959a2b8da558ce117634660440 Mon Sep 17 00:00:00 2001 From: Ole Tange Date: Fri, 9 Sep 2011 21:15:00 +0200 Subject: [PATCH] parallel: Implemented --shellquote --- doc/FUTURE_IDEAS | 5 +++++ src/parallel | 16 ++++++++++++++-- src/parallel.pod | 8 ++++++++ testsuite/tests-to-run/test65.sh | 5 +++++ testsuite/wanted-results/test65 | 2 ++ 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/doc/FUTURE_IDEAS b/doc/FUTURE_IDEAS index af98db64..5453844b 100644 --- a/doc/FUTURE_IDEAS +++ b/doc/FUTURE_IDEAS @@ -7,6 +7,11 @@ niceload --start-condition Til QUOTING: +cat <<'_EOF' | parallel -v echo +awk -v FS="\",\"" '{print $1, $3, $4, $5, $9, $14}' | grep -v "#" | sed -e '1d' -e 's/\"//g' -e 's/\/\/\//\t/g' | cut -f1-6,11 | sed -e 's/\/\//\t/g' -e 's/ /\t/g +_EOF + + FN="two spaces" echo 1 | parallel -q echo {} "$FN" # Prints 2 spaces between 'two' and 'spaces' diff --git a/src/parallel b/src/parallel index 715eea76..d8b40fee 100755 --- a/src/parallel +++ b/src/parallel @@ -397,6 +397,7 @@ sub options_hash { "number-of-cpus" => \$::opt_number_of_cpus, "number-of-cores" => \$::opt_number_of_cores, "use-cpus-instead-of-cores" => \$::opt_use_cpus_instead_of_cores, + "shellquote|shell_quote|shell-quote" => \$::opt_shellquote, "nice=i" => \$::opt_nice, "timeout=i" => \$::opt_timeout, "onall" => \$::opt_onall, @@ -841,7 +842,7 @@ sub __QUOTING_ARGUMENTS_FOR_SHELL__ {} sub shell_quote { my @strings = (@_); for my $a (@strings) { - $a =~ s/([\002-\011\013-\032\\\#\?\`\(\)\*\>\<\~\|\; \"\!\$\&\'])/\\$1/g; + $a =~ s/([\002-\011\013-\032\\\#\?\`\(\)\{\}\*\>\<\~\|\; \"\!\$\&\'])/\\$1/g; $a =~ s/[\n]/'\n'/g; # filenames with '\n' is quoted using \' } return wantarray ? @strings : "@strings"; @@ -852,7 +853,7 @@ sub shell_quote_scalar { # Returns: # string quoted with \ as needed by the shell my $a = shift; - $a =~ s/([\002-\011\013-\032\\\#\?\`\(\)\*\>\<\~\|\; \"\!\$\&\'])/\\$1/g; + $a =~ s/([\002-\011\013-\032\\\#\?\`\(\)\{\}\*\>\<\~\|\; \"\!\$\&\'])/\\$1/g; $a =~ s/[\n]/'\n'/g; # filenames with '\n' is quoted using \' return $a; } @@ -3701,6 +3702,11 @@ sub len { # Worse than worst case: every char needs to be quoted with \ $len *= 2; } + if($::opt_nice) { + # Pessimistic length if --shellquote is set + # Worse than worst case: every char needs to be quoted with \ twice + $len *= 4; + } return $len; } @@ -3795,6 +3801,12 @@ sub replaced { $self->{'replaced'} = "nice -n" . $::opt_nice . " bash -c " . ::shell_quote_scalar($self->{'replaced'}); } + if($::opt_shellquote) { + # Prepend echo + # and quote twice + $self->{'replaced'} = "echo " . + ::shell_quote_scalar(::shell_quote_scalar($self->{'replaced'})); + } } if($::oodebug and length($self->{'replaced'}) != ($self->len())) { ::my_dump($self); diff --git a/src/parallel.pod b/src/parallel.pod index 99bc0c4e..0112a94f 100644 --- a/src/parallel.pod +++ b/src/parallel.pod @@ -879,6 +879,8 @@ default. If the stdin (standard input) only contains whitespace, do not run the command. +If used with B<--pipe> this is slow. + =item B<--recstart> I @@ -1036,6 +1038,12 @@ Use the replacement string I instead of B<{#}> for job sequence number. +=item B<--shellquote> + +Quote input as would be needed by the shell. Useful for making quoting +composed commands for GNU B. + + =item B<--skip-first-line> Do not use the first line of input (used by GNU B itself diff --git a/testsuite/tests-to-run/test65.sh b/testsuite/tests-to-run/test65.sh index 346a807b..73290d98 100644 --- a/testsuite/tests-to-run/test65.sh +++ b/testsuite/tests-to-run/test65.sh @@ -5,3 +5,8 @@ cat <<'EOF' | sed -e s/\$SERVER1/$SERVER1/\;s/\$SERVER2/$SERVER2/ | parallel -j1 echo '### Test --timeout'; parallel -j0 -k --timeout 1 echo {}\; sleep {}\; echo {} ::: 1.1 2.2 3.3 4.4 EOF + +echo '### Test --shellquote' +cat <<'_EOF' | parallel --shellquote +awk -v FS="\",\"" '{print $1, $3, $4, $5, $9, $14}' | grep -v "#" | sed -e '1d' -e 's/\"//g' -e 's/\/\/\//\t/g' | cut -f1-6,11 | sed -e 's/\/\//\t/g' -e 's/ /\t/g +_EOF diff --git a/testsuite/wanted-results/test65 b/testsuite/wanted-results/test65 index 98793fb6..e09d70f5 100644 --- a/testsuite/wanted-results/test65 +++ b/testsuite/wanted-results/test65 @@ -4,3 +4,5 @@ 2.2 3.3 4.4 +### Test --shellquote +awk\ -v\ FS=\"\\\",\\\"\"\ \'\{print\ \$1,\ \$3,\ \$4,\ \$5,\ \$9,\ \$14\}\'\ \|\ grep\ -v\ \"\#\"\ \|\ sed\ -e\ \'1d\'\ -e\ \'s/\\\"//g\'\ -e\ \'s/\\/\\/\\//\\t/g\'\ \|\ cut\ -f1-6,11\ \|\ sed\ -e\ \'s/\\/\\//\\t/g\'\ -e\ \'s/\ /\\t/g