cha*_*imp 12 parallel-processing bash args
我需要将一些包含空格和其他字符的文本传递给由GNU Parallel运行的脚本.
这是一个非常简单的例子:
$ seq 1 3 | parallel echo "Quoted ' (text)"
Run Code Online (Sandbox Code Playgroud)
上面的例子将输出:
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
Run Code Online (Sandbox Code Playgroud)
但是,如果我这样做,一切正常:
seq 1 3 | parallel echo "\"Quoted ' (text)\""
Run Code Online (Sandbox Code Playgroud)
我碰巧是从python脚本运行它,所以在传递参数之前,我在脚本中双引用它们,如下所示:
args = ["Some arg", "Another arg", "etc."]
args = ' '.join(pipes.quote(pipes.quote(arg)) for arg in args)
Run Code Online (Sandbox Code Playgroud)
但它似乎不是一个干净的解决方案.
有没有人知道将参数传递给GNU Parallel的更好方法?
谢谢!
Dim*_*lov 13
zsh-4.3.12[sysadmin]% print -l {1..3} |
parallel -q echo "Quoted ' (text)"
Quoted ' (text) 1
Quoted ' (text) 2
Quoted ' (text) 3
Run Code Online (Sandbox Code Playgroud)
如@mortehu所述:
通过并行传递给命令的参数由shell扩展两次:一次在并行调用中,一次在并行运行命令时.
-q
防止第二个shell扩展.
Ole*_*nge 11
手册页中有一整段用于引用:
http://www.gnu.org/s/parallel/man.html#QUOTING
它甚至提到了你在问题中写的错误信息.
如果您可以更好地编写它,请将您的版本发送至:parallel@gnu.org.