bash 一个接一个地运行命令,即使我挂起第一个命令 (Ctrl-z)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13600319/
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
Run one command after another, even if I suspend the first one (Ctrl-z)
提问by asmeurer
I know in bash I can run one command after another by separating them by semicolons, like
我知道在 bash 中,我可以通过用分号分隔来一个接一个地运行命令,例如
$ command1; command2
Or if I only want command2
to run only if command1
succeeds, using &&
:
或者,如果我只想command2
在command1
成功时运行,请使用&&
:
$ command1 && command2
This works, but if I suspend command1
using Ctrl-z
, in the first case, it runs command2
immediately, and in the second case, it doesn't run it at all. How can I run commands in sequence, but still be able to suspend the first command, but not have the second run until I have restarted it (with fg
) and it finishes? I'd prefer something as simple to type as possible, as I would like to do this interactively. Or maybe I just need to set an option somewhere.
这有效,但如果我暂停command1
using Ctrl-z
,在第一种情况下,它会command2
立即运行,而在第二种情况下,它根本不运行。如何按顺序运行命令,但仍然能够挂起第一个命令,但在我重新启动它(使用fg
)并完成之前不能进行第二次运行?我更喜欢输入尽可能简单的东西,因为我想以交互方式进行。或者也许我只需要在某处设置一个选项。
By the way, what is the proper term for what Ctrl-z
does?
顺便说一句,什么是什么的正确术语Ctrl-z
?
回答by NPE
The following should do it:
以下应该这样做:
(command1; command2)
Note the added parentheses.
请注意添加的括号。
回答by cdarke
In Bash, when you place a job into the background (using CTRL+Z or &) it does not wait for the job to finish, and gives an exit code of zero (success). That much you have observed, and it is documented in the man
pages.
在 Bash 中,当您将作业置于后台时(使用 CTRL+Z 或 &),它不会等待作业完成,并给出零退出代码(成功)。您已经观察到了这么多,并且记录在man
页面中。
The behaviour of logical "AND", &&, is that it tests from left-to right. Each part must be successful, so if the first is unsuccessful then the second will not run. So with && it runs commands from left to right until one of them fails. The definition of success is an exitcode ($?) of zero.
逻辑“与”的行为,&&,是从左到右进行测试。每个部分都必须成功,所以如果第一个不成功,那么第二个将不会运行。所以使用 && 它从左到右运行命令,直到其中一个失败。成功的定义是退出代码 ($?) 为零。
Contrast this with logical "OR", ||, which runs commands from left to right until one of them works.
将此与逻辑“或”,|| 进行对比,后者从左到右运行命令,直到其中之一起作用。
Explaination of the subshell solution give by @NPE can also be found in the man
pages:
@NPE 给出的 subshell 解决方案的解释也可以在man
页面中找到:
Compound commands and command sequences of the form ‘a ; b ; c' are not handled gracefully when process suspension is attempted. When a process is stopped, the shell immediately executes the next command in the sequence. It suffices to place the sequence of commands between parentheses to force it into a subshell, which may be stopped as a unit.
'a ; 形式的复合命令和命令序列 乙; 当尝试暂停进程时,c' 没有得到妥善处理。当一个进程停止时,shell 立即执行序列中的下一个命令。将命令序列放在括号之间就足以将其强制放入子外壳中,该子外壳可以作为一个单元停止。
The proper term for CTRL+Z is the suspendcharacter, again from the man
pages:
CTRL+Z 的正确术语是暂停字符,同样来自man
页面:
Typing the suspend character (typically ^Z, Control-Z) while a process is running causes that process to be stopped and returns control to bash.
在进程运行时输入挂起字符(通常是 ^Z、Control-Z)会导致该进程停止并将控制权返回给 bash。
(Sorry to quote the man
pages so much, but they really are your friends and worth reading)
(很抱歉引用了man
这么多页面,但它们确实是您的朋友,值得一读)
If you look at stty -a
you will see something like this:
如果你看,stty -a
你会看到这样的东西:
susp = ^Z;
So you can alter it, hence the phrase "typically". Don't do that though, it will confuse the heck out of everyone. The terminal driver raises a SIGTSTP signal which is trapped by Bash.
所以你可以改变它,因此短语“典型地”。但是不要这样做,它会让每个人都感到困惑。终端驱动程序发出 SIGTSTP 信号,该信号被 Bash 捕获。