Clo*_*ONG 13 command-line bash
我遇到了一个头痛的问题。
我想在后台执行多个命令,所以我想在 bash 中一一启动它们。在后台启动linux shell中的一个命令很容易,就像这样:
myCommand &
Run Code Online (Sandbox Code Playgroud)
启动多个命令也很容易,就像这样:
myCommand1 && myCommand2
Run Code Online (Sandbox Code Playgroud)
或者
myCommand1 ; myCommand2
Run Code Online (Sandbox Code Playgroud)
但是如果我想在后台运行多个命令,我尝试了以下命令格式,但失败了:
myCommand1 & && myCommand2 &
Run Code Online (Sandbox Code Playgroud)
或者
myCommand1 & ; myCommand2 &
Run Code Online (Sandbox Code Playgroud)
两种格式都失败。如何&
在一个命令行中运行多个命令?
Rin*_*ind 28
用 ()。
如果要按顺序运行它们:
(myCommand1; myCommand2) &
Run Code Online (Sandbox Code Playgroud)
或者
(myCommand1 &) && (myCommand2 &)
Run Code Online (Sandbox Code Playgroud)
如果您希望它们并行运行:
myCommand1 & myCommand2 &
Run Code Online (Sandbox Code Playgroud)
在 bash 中,您也可以使用它({ 和 ; 后面的空格是必需的):
{ myCommand1 && myCommand2; } &
Run Code Online (Sandbox Code Playgroud)
des*_*ert 19
我想你想要这个:
myCommand1 & myCommand2 &
Run Code Online (Sandbox Code Playgroud)
这将启动myCommand1
并将其发送到后台,因为它后跟与符号,然后立即启动myCommand2
并将其发送到后台,因此再次释放外壳。
为了更好地理解,您可以在此处通过命令替换管道。
列表是由一个操作符分隔的一个或多个管道的序列;, & , && , 或|| ,并可选地以其中之一终止 ;, & , 或 .
如果命令由控制运算符&终止,shell 将在子 shell 的后台执行该命令。shell 不等待命令完成,返回状态为 0。命令以;分隔。依次执行;shell 依次等待每个命令终止。返回状态是最后执行的命令的退出状态。
AND 和 OR 列表是由&& 和 ||分隔的一个或多个管道的序列 分别控制运算符。
来源:man bash
让我们将其分解为示例。您可以通过组合命令并使用以下命令之一将它们分开来构建列表; & && ||
:
command1 ; command2 # runs sequentially
command1 && command2 # runs sequentially, runs command2 only if command1 succeeds
command1 || command2 # runs sequentially, runs command2 only if command1 fails
command1 & command2 # runs simultaneously
Run Code Online (Sandbox Code Playgroud)
您可以使用以下之一终止列表:; & <newline>
。
通常,您通过按 来执行命令或列表Enter,即等于<newline>
。分号;
具有相同的用途,尤其是在脚本中。&
然而,&符号在后台的子shell中启动命令,立即释放shell。
您可以使用()
圆括号或大括号{}
进一步对列表进行分组,不同之处在于圆括号生成子外壳,而大括号则不会。大括号在第一个之后需要一个空格,在结束括号之前需要一个分号或换行符。例如:
# if c1 succeeds start a shell in the background
# and run c2 and c3 sequentially inside it
c1 && ( c2 ; c3 ) &
# run c1 and if it succeeds c2 sequentially as a group command
# if c1 or c2 fail run c3 in the background
{ c1 && c2 ;} || c3 &
Run Code Online (Sandbox Code Playgroud)
如果您不确定使用true
并false
测试构造是否按预期工作,这可能会变得非常复杂:
$ { true && true ;} || echo 2
$ { true && false ;} || echo 2
2
Run Code Online (Sandbox Code Playgroud)
该jobs
命令显示当前 shell 中正在运行或最近完成的后台作业的列表。有许多用于作业控制的键盘快捷键和命令:
jobs
列表中fg
=%
必要时将进程带入前台启动它,您可以按如下方式指定进程:
% # last process in the jobs list
%1 # 1st process in the jobs list
%abc # process beginning with the string “abc”
%?abc # process containing the string “abc” anywhere
Run Code Online (Sandbox Code Playgroud)bg
=%&
将进程带入后台,在必要时启动它:
%& # last process in the jobs list
%1& # 1st process in the jobs list
%abc& # process beginning with the string “abc”
%?abc& # process containing the string “abc” anywhere
Run Code Online (Sandbox Code Playgroud)wait
等待后台进程完成并返回其终止状态:
wait %1 # 1st process in the jobs list
Run Code Online (Sandbox Code Playgroud)
想象一下,您开始了一个冗长的进程(jobs
显示它是第 3 个),然后意识到您希望计算机在它完成时挂起,echo
如果进程没有成功,再加上一条消息:
wait %3 || echo failed ; systemctl suspend
Run Code Online (Sandbox Code Playgroud) 归档时间: |
|
查看次数: |
46145 次 |
最近记录: |