防止 GNU 并行分割引用的参数

Feu*_*mel 4 command-line quoting gnu-parallel

我正在尝试使用GNU Parallel结合常量和变化的参数来多次运行comman。但由于某种原因,即使我在将常量参数传递给parallel.

在此示例中,常量参数应作为单个参数而不是两个参数'a b'传递:debug-call

$ parallel debug-call 'a b' {} ::: {1..2}
[0] = '[...]/debug-call'
[1] = 'a'
[2] = 'b'
[3] = '1'
[0] = '[...]/debug-call'
[1] = 'a'
[2] = 'b'
[3] = '2'
Run Code Online (Sandbox Code Playgroud)

debug-call是一个简单的脚本,它打印传入的每个参数argv。相反,我希望看到这个输出:

[0] = '[...]/debug-call'
[1] = 'a b'
[2] = '1'
[0] = '[...]/debug-call'
[1] = 'a b'
[2] = '2'
Run Code Online (Sandbox Code Playgroud)

这是一个错误还是有一个选项可以阻止 GNU Parallel 在将命令行参数传递给命令之前分割它们?

Sté*_*las 5

parallel运行一个 shell(具体取决于调用它的上下文,通常,当从 shell 调用时,它是同一个 shell)来解析参数的串联。

所以:

parallel debug-call 'a b' {} ::: 'a b' c
Run Code Online (Sandbox Code Playgroud)

是相同的

parallel 'debug-call a b {}' ::: 'a b' c
Run Code Online (Sandbox Code Playgroud)

parallel将会通知:

your-shell -c 'debug-call a b <something>'
Run Code Online (Sandbox Code Playgroud)

<something>(希望)该 shell 的参数在哪里正确引用。例如,如果该 shell 是bash,它将运行

bash -c 'debug-call a b a\ b'
Run Code Online (Sandbox Code Playgroud)

在这里,您想要:

parallel 'debug-call "a b" {}' ::: 'a b' c
Run Code Online (Sandbox Code Playgroud)

或者

parallel -q debug-call 'a b' {} ::: 'a b' c
Run Code Online (Sandbox Code Playgroud)

在连接之前, Whereparallel将引用参数(以 shell 的正确(希望)语法)。

为了避免首先调用 shell,您可以使用 GNUxargs来代替:

xargs -n1 -r0 -P4 -a <(printf '%s\0' 'a b' c) debug-call 'a b'
Run Code Online (Sandbox Code Playgroud)

这不会调用 shell(也不会调用初始化时运行的任何命令parallel),但您不会从 的任何额外功能中受益parallel,例如使用 进行输出重新排序-k

您可能会在后台并行执行中找到其他方法

  • 谢谢你的回答。`-q` 似乎有效。是否有可能完全阻止我的 shell 被调用?即让“parallel”将参数直接传递给“execvp(3)”或类似的? (2认同)