parallel: Implemented --shellquote

This commit is contained in:
Ole Tange 2011-09-09 21:15:00 +02:00
parent 795c0fa3bd
commit c48c5fb7b9
5 changed files with 34 additions and 2 deletions

View file

@ -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'

View file

@ -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);

View file

@ -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<startstring>
@ -1036,6 +1038,12 @@ Use the replacement string I<replace-str> 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<parallel>.
=item B<--skip-first-line>
Do not use the first line of input (used by GNU B<parallel> itself

View file

@ -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

View file

@ -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