关于linux命令的问题

发布于 2024-08-18 19:45:24 字数 76 浏览 4 评论 0原文

以下命令中的两个 & 符号有何作用:

(make foo&)&

What do the two ampersands in the following command do:

(make foo&)&

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不弃不离 2024-08-25 19:45:24

在子 shell 中运行命令。这意味着将生成一个单独的 shell 并运行该命令。这可能是因为他们想使用 shell 特定的操作(背景 - 其他示例是重定向等)。命令中的第一个 & 是在子 shell 中运行的命令的后台(即 make foo)。第二个 & 符号将子 shell 本身设置为背景,以便您立即返回命令提示符。

您可以在此处查看效果

当前 shell 上的前景

(bb-python2.6)noufal@NibrahimT61% ls # shell waits for process to complete
a  b  c  d  e

当前 shell 上的背景

(bb-python2.6)noufal@NibrahimT61% ls& #Shell returns immediately.
[1] 3801
a  b  c  d  e
[1]  + done       /bin/ls -h -p --color=auto -X

使用子 shell(前景)

(bb-python2.6)noufal@NibrahimT61% (ls&) # Current shell waits for subshell to finish.
a  b  c  d  e                           

在这种情况下,当前 shell 会等待子 shell 完成,即使子 shell 中的作业本身已在后台运行。

使用子 shell (BAckground)

(bb-python2.6)-130- noufal@NibrahimT61% (ls &)&
[1] 3829
a  b  c  d  e
[1]  + exit 130   (; /bin/ls -h -p --color=auto -X &; )

前台 shell 立即返回(不等待子 shell,子 shell 本身不等待 ls 完成)。观察执行的命令的差异。


关于需要在子 shell 中运行某些命令的旁注。假设您想运行一个“shell 命令”(即使用 shell 特定内容(如重定向、作业 ID 等)),您必须在子 shell 中运行该命令(使用 ( , )) 或使用 bash 等 shell 的 -c 选项。你不能直接exec这样的事情,因为 shell 是处理作业 ID 或其他东西所必需的。使用“&”将使子 shell 立即返回。代码中的第二个&符号看起来(如其他答案所示)是多余的。一个“确保它是背景的”的例子。

The ( and ) run the command in a subshell. This means that a separate shell is spawned off and the command is run. This is probably because they wanted to use shell specific operation (backgrounding - other examples are redirection etc.). The first & in the command backgrounds the command run in the subshell (ie. make foo). The second ampersand backgrounds the subshell itself so that you get back your command prompt immediately.

You can see the effects here

Foreground on the current shell

(bb-python2.6)noufal@NibrahimT61% ls # shell waits for process to complete
a  b  c  d  e

Background on the current shell

(bb-python2.6)noufal@NibrahimT61% ls& #Shell returns immediately.
[1] 3801
a  b  c  d  e
[1]  + done       /bin/ls -h -p --color=auto -X

Using a subshell (Foreground)

(bb-python2.6)noufal@NibrahimT61% (ls&) # Current shell waits for subshell to finish.
a  b  c  d  e                           

In this case, the current shell waits for the subshell to finish even though the job in the subshell itself is backgrounded.

Using a subshell (BAckground)

(bb-python2.6)-130- noufal@NibrahimT61% (ls &)&
[1] 3829
a  b  c  d  e
[1]  + exit 130   (; /bin/ls -h -p --color=auto -X &; )

The foreground shell returns immediately (Doesn't wait for the subshell which itself doesn't wait for the ls to finish). Observe the difference the command executed.


A sidenote on the need to run some commands in a subshell. Suppose you wanted to run a "shell command" (ie. One that uses shell specific stuff like redirects, job ids etc.), you'd have to either run that command in a subshell (using (, )) or using the -c option to shells like bash. You can't just directly exec such things because the shell is necessary to process the job id or whatever. Ampersanding that will have the subshell return immediately. The second ampersand in your code looks (like the other answer suggests) redundant. A case of "make sure that it's backgrounded".

℡寂寞咖啡 2024-08-25 19:45:24

没有上下文很难说,但是&在 shell 命令中,该命令在后台运行并立即继续,无需等待命令完成。也许 Makefile 作者想要并行运行多个命令。不过,第二个 & 符号是多余的,括号也是多余的。

It's difficult to say without context, but & in shell commands runs the command in the background and immediately continues, without waiting for the command to finish. Maybe the Makefile author wanted to run several commands in parallel. The second ampersand would be redundant though, as are the parentheses.

楠木可依 2024-08-25 19:45:24

& 符号在 makefile 中用作行继续符。

很难确定,因为您的问题没有足够的上下文。

Ampersand is used as a line continuation character in makefiles.

Hard to say for sure since there isn't enough context in your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文