`lxc exec` 中的双连字符有什么作用

Jon*_* Y. 4 lxc lxd

一些教程(例如linuxcontainers.org 上的入门)建议在用于在 LXD 容器内运行程序--时使用双连字符 ( ) lxc exec,如

$ lxc exec my-container -- apt update
Run Code Online (Sandbox Code Playgroud)

我怀疑--告诉lxc exec将提供的标志传递给程序,因为例如lxc exec my-container apt list --upgradable失败

error: flag provided but not defined: --upgradable
Run Code Online (Sandbox Code Playgroud)

但是,我找不到有关该问题的任何文档。有人可以确认或澄清这是否不完全是它的作用吗?

此外,我看到人们--在不同地方使用的实例,例如:

$ lxc exec my-container apt -- list --upgradable
$ lxc exec my-container apt list -- --upgradable
Run Code Online (Sandbox Code Playgroud)

有什么不同?

小智 5

部分地,这与双破折号的含义重复。

你问了一个有趣的问题lxc,让我来回答一下。在您的两个示例中,这两个实例--告诉lxc在此之后不解释选项。在这种情况下,不同的定位是无关紧要的。但是,由于您在容器中运行命令行,因此您可能希望将其--用作该命令行的一部分。考虑以下内容:我创建了一个/tmp/bar包含行--foo. 在容器中运行了以下命令:

root@myContainer:~# echo "here is --foo" >/tmp/bar
root@myContainer:~# echo "this line has no double dash" >> /tmp/bar
root@myContainer:~# cat /tmp/bar
here is --foo
this line has no double dash
root@myContainer:~# exit
Run Code Online (Sandbox Code Playgroud)

现在我将使用grep. 我需要grep知道这--是搜索模式的一部分,而不是以下选项grep

$ lxc exec myContainer -- grep -- --foo /tmp/bar
here is --foo
Run Code Online (Sandbox Code Playgroud)

如果我使用单个--它会被lxc命令行消耗并且我收到错误:

$ lxc exec myContainer grep -- --foo /tmp/bar
grep: unrecognized option '--foo'
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
Run Code Online (Sandbox Code Playgroud)