问题描述
我有cmd2
,在cmd1
完成后需要遵循.有时我需要暂停cmd1
.
I have cmd2
that needs to follow after cmd1
completes. I need to pause cmd1
sometimes.
我输入
$ cmd1 && cmd2
,然后按Ctrl+Z (Stop)
键停止cmd1
.现在,cmd1
已暂停,但是当我恢复时,它在cmd1
完成后不会启动cmd2
.
and then press Ctrl+Z (Stop)
to stop cmd1
. Now, cmd1
is paused but when I resume, it does not start cmd2
after completion of cmd1
.
我输入
$ cmd1 ; cmd2
,然后按Ctrl+Z (Stop)
停止cmd1
.现在cmd1
已暂停,但立即开始cmd2
.但是,我希望仅在cmd1
完成后才开始cmd2
.
and then I press Ctrl+Z (Stop)
to stop cmd1
. Now cmd1
is paused but it immediately starts cmd2
. However, I wish to start cmd2
only after cmd1
finishes.
我做了一些研究,有人在zsh
中提出了一种优雅的方法,但是我想知道在bash
中是否有一种优雅的方法.
I did some research and someone suggested an elegant way in zsh
but I wonder if there is an elegant way of doing it in bash
.
推荐答案
在子shell中运行它:
Run it in a subshell:
(cmd1 && cmd2)
示例:
$ (sleep 5 && echo 1) # started the command chain
^Z
[1]+ Stopped ( sleep 5 && echo 1 ) # stopped before `sleep 5` finished
$ fg # resumed
( sleep 5 && echo 1 )
1 # `sleep 5` finished and `echo 1` ran
这篇关于如何在Bash中暂停和恢复一系列命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!