16

我需要将一些包含空格和其他字符的文本传递给由 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 的更好方法吗?

谢谢!

4

2 回答 2

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

正如@mortehu 所述:

通过并行传递给命令的参数由 shell 扩展两次:一次在并行调用中,一次在并行运行命令时。-q防止第二次壳膨胀。

于 2011-11-22T17:26:42.027 回答
13

手册页中有一个专门用于引用的部分:

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

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

如果你能写得更好,请将你的版本通过电子邮件发送到:parallel@gnu.org。

于 2011-11-23T15:45:06.890 回答