bash 如何将引用的参数传递给 GNU Parallel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8230074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:05:50  来源:igfitidea点击:

How to pass quoted args to GNU Parallel

bashparallel-processingargs

提问by chaimp

I need to pass some text that includes whitespace and other characters to a script that's being run by GNU Parallel.

我需要将一些包含空格和其他字符的文本传递给由 GNU Parallel 运行的脚本。

Here is a very simple example:

这是一个非常简单的例子:

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

The above example will output this:

上面的例子将输出:

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    

However, if I do this everything works:

但是,如果我这样做,一切正常:

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

I happen to be running this from a python script, so before passing the arguments I'm double quoting them in the script like this:

我碰巧从 python 脚本运行它,所以在传递参数之前,我在脚本中双引号它们是这样的:

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

But it doesn't seem like a clean solution.

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

Does anybody know of a better way to pass arguments to GNU Parallel?

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

Thanks!

谢谢!

回答by Dimitre Radoulov

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

As described by @mortehu:

正如@mortehu 所描述的:

Arguments passed to commands through parallel are expanded by the shell twice: once in the invocation of parallel, and once when parallel runs your command. -qprevents the second shell expansion.

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

回答by Ole Tange

There is a whole section in the man page dedicated to quoting:

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

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

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

It even mentions the very error messages you write in your question.

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

If you can write it better please email your version to: [email protected].

如果您能写得更好,请将您的版本通过电子邮件发送至:[email protected]