Answers:
您无需在脚本中添加任何内容。外壳允许这种功能。
T,已停止)jobs终端或列表中的所有已停止的作业。fg(前景)。它将作业恢复回到前台进程组,并且作业继续运行。看一个例子:
root@host:~$ sleep 10 # sleep for 10 seconds
^Z
[1]+  Stopped                 sleep 10
root@host:~$ jobs # list all stopped jobs
[1]+  Stopped                 sleep 10
root@host:~$ fg # continue the job
sleep 10
root@host:~$ # job has finishedcmd; cmd; cmd;就像写作cmd <newline> cmd <newline> ...。( cmd; cmd; cmd; )除了您可以编写的脚本外,它的行为类似于脚本,因为由(
                    sleep 10。当我在3秒钟后按CTRL + Z并在几秒钟后恢复时,发现睡眠命令在不到7秒钟内死亡。这与您所说的相反,因为命令永远不会停止,它只会在后台运行。
                    strace了sleep命令,发现使用的系统调用是nanosleep()。这似乎是系统调用的定义行为nanosleep。restart_syscall()使用时间参数重新启动被中断的系统调用,该参数经过适当调整以解决已经过去的时间(包括信号停止过程的时间)。阅读该手册页:man7.org/linux/man-pages/man2/restart_syscall.2.html
                    fg %1或背景(例如:)中bg %1。(如果作业仅给出1个数字,即只有1个暂停的进程,如示例混乱所示:only [1]+ stopped  sleep 10,则可以省略该%n零件。如果有多个后台进程(正在运行或已停止),则需要指定所需的一个与:(%n例如:   fg %2  让%2在前景中恢复))
                    如果只想暂停脚本而又保留在脚本中,则可以使用read而不是sleep。
您可以使用
read -t设置读取超时
read -n以读取一个字符(实际上只需按任意键)即可继续执行脚本
由于您未提供任何代码,因此下面是如何使用它的示例。
如果按q,则read -n1阻止脚本继续运行,直到按任意键。
按下某个键后,将重置检查,脚本将照常在循环中继续。
while [[ true ]]; do
    read -t2 -n1 check
    if [[ $check == "q" ]];then
        echo "pressed"
        read -n1
        check=""
    else
        echo "not pressed"
    fi
echo "Doing Something"
done您还可以添加stty -echo到本节的开头和stty echo末尾,以防止打字弄乱屏幕输出
这样dd您就可以可靠地从文件中读取单个字节。使用,stty您可以设置min一定数量的字节以限定终端读取和time输出的十分之一秒。结合这两者sleep,我想您可以完全不用做,而只需让终端的读取超时为您完成工作即可:
s=$(stty -g </dev/tty)
(while stty raw -echo isig time 20 min 0;test -z "$(
dd bs=1 count=1 2>/dev/null; stty "$s")" || (exec sh)
do echo "$SECONDS:" do your stuff here maybe                             
   echo  no sleep necessary, I think                                                          
   [ "$((i+=1))" -gt 10 ] && exit                                                             
done       
) </dev/tty这是一个小示例while循环,我为您模拟了该循环。dd尝试读取stdin-从/dev/tty- 重定向- 每两秒钟超时,while循环循环。超时或dd 不超时,因为您按下了键-在这种情况下,将调用交互式外壳。
这是一个测试运行-每行开头打印的数字是shell变量的值$SECONDS:
273315: do your stuff here maybe
no sleep necessary, I think
273317: do your stuff here maybe
no sleep necessary, I think
273319: do your stuff here maybe
no sleep necessary, I think
273321: do your stuff here maybe
no sleep necessary, I think
sh-4.3$ : if you press a key you get an interactive shell
sh-4.3$ : this example loop quits after ten iterations
sh-4.3$ : or if this shell exits with a non-zero exit status
sh-4.3$ : and speaking of which, to do so you just...
sh-4.3$ exit
exit
273385: do your stuff here maybe
no sleep necessary, I think
273387: do your stuff here maybe
no sleep necessary, I think
273389: do your stuff here maybe
no sleep necessary, I think
273391: do your stuff here maybe
no sleep necessary, I think
273393: do your stuff here maybe
no sleep necessary, I think
273395: do your stuff here maybe
no sleep necessary, I think
273397: do your stuff here maybe
no sleep necessary, I thinks=$(stty -g </dev/tty)。致电后,dd我立即用恢复stty "$s"。终端状态不关心子外壳,因此无论父外壳如何,这些设置都会保留下来。stty sane不一定是您要执行的操作-最好将状态恢复为找到状态的方式,而不要假设当时的状态sane。如果我不还原它,那些echos到处都是。弄清楚这一点是我来这么晚的部分原因-当我开始测试时,您的答案就没有在这里了。
                    
sleep 10; notify-send hello并按CTRL + Z停止,请notify-send hello执行。如果第二个命令正在执行,第一个进程如何停止?在那之后,如果fg我看不到任何事情发生,这是显而易见的,因为第二条命令已经执行了