如何将带引号的参数传递给GNU Parallel

17
我需要将包含空白及其他字符的文本传递给由GNU Parallel运行的脚本。

下面是一个非常简单的示例:

$ seq 1 3 | parallel echo "Quoted ' (text)"

上面的示例将输出如下内容:

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    

然而,如果我这样做一切都可以正常工作:

seq 1 3 | parallel echo "\"Quoted ' (text)\""

我碰巧是从Python脚本中运行的,因此在传递参数之前,我会像这样在脚本中对它们进行双引号括起来:

args = ["Some arg", "Another arg", "etc."]
args = ' '.join(pipes.quote(pipes.quote(arg)) for arg in args)

但这似乎不是一个干净的解决方案。

有人知道更好的将参数传递给 GNU Parallel 的方法吗?

谢谢!


不是一个完美的解决方案,但它能工作吗?祝你好运。 - shellter
2个回答

22
zsh-4.3.12[sysadmin]% print -l {1..3} | 
  parallel -q echo "Quoted ' (text)"
Quoted ' (text) 1
Quoted ' (text) 2
Quoted ' (text) 3

如 @mortehu 所述:

通过 parallel 传递给命令的参数在 shell 中会被扩展两次:一次是在调用 parallel 时,一次是在 parallel 运行你的命令时。使用 -q 可以防止第二次 shell 扩展。


2
谢谢!我查看了man页面,但找不到这么简单的东西。感谢您清晰地回答,而不是像“阅读man页面!”那样说。如此简单。 - chaimp
2
为了让这更加清晰:通过 parallel 传递给命令的参数会被 shell 扩展两次:一次是在调用 parallel 时,另一次是在 parallel 运行您的命令时。-q 可以防止第二次 shell 扩展。 - mortehu

12

在man页面中有一个完整的章节专门介绍引用:

http://www.gnu.org/s/parallel/man.html#QUOTING

它甚至提到了你在问题中写的错误信息。

如果您能够更好地编写,请将您的版本发送电子邮件至:parallel@gnu.org。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接