一个命令行循环内BG / FG [英] bg / fg inside a command line loop

查看:154
本文介绍了一个命令行循环内BG / FG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CTRL-Z(^ Z)作用的方式从终端执行的循环中完成时,我不明白。

ctrl-z (^z) acts in ways I do not understand when done inside a loop executed from a terminal.

说我键入

for ii in {0..100}; do echo $ii; sleep 1; done

然后我打^ Z。我会得到:

then I hit ^z. I'll get:

[1]+  Stopped                 sleep 1

我可以继续使用FG或BG的工作,但只有他睡命令作业是指。循环的其余部分已明显消失,并没有更多的数字出现在终端上。

I can resume the job using fg or bg, but the job refers only to he sleep command. The rest of the loop has apparently disappeared, and no more number appear on the terminal.

我可以用&放大器;命令后,立即在后台运行,或者另一种解决方案是包装在子shell整个事情:

I could use & after the command to immediately run it in the background, or another solution is to wrap the whole thing in a subshell:

( for ii in {0..100}; do echo $ii; sleep 1; done )

然后^ Z给了我

then ^z gives me

[1]+  Stopped                 ( for ii in {0..100};
do
    echo $ii; sleep 1;
done )

此作业可继续进行,大家都高兴。但我不是一般运行一个一次性的任务时,这样做的习惯,我问的问题是,为什么第一个行为首先发生。有没有办法中止未subshel​​l'd命令行循环?并且发生了什么环路的在第一实施例的其余部分?

This job can be resumed and everyone is happy. But I'm not generally in the habit of doing this when running a one-off task, and the question I am asking is why the first behavior happens in the first place. Is there a way to suspend a command-line loop that isn't subshell'd? And what happened to the rest of the loop in the first example?

请注意,这是具体的循环:

Note that this is specific to the loop:

echo 1; sleep 5; echo 2

和睡眠时打^ Z导致回声2 来执行:

and hitting ^z during the sleep causes the echo 2 to execute:

1
^Z
[2]+  Stopped                 sleep 5
2

或者我应该得到使用和放大器的习惯;并称之为黑暗魔法?

Or should I just get in the habit of using & and call it dark magic?

推荐答案

您不能暂停当前shell 的执行。当您运行在命令行中你的循环,它在当前的登录shell /终端执行。当你preSS [Ctrl + Z]你是在告诉shell将暂停当前活动进程。你的循环简直就是在当前shell的计数器,正在执行过程睡眠。暂停只能对睡眠。

You cannot suspend execution of the current shell. When you run your loop from the command line, it is executing in your current login shell/terminal. When you press [ctrl+z] you are telling the shell to suspend the current active process. Your loop is simply a counter in the current shell, the process being executed is sleep. Suspend only operates on sleep.

当你BACKGROUD进程或在子shell(大致相当于)执行它,你可以暂停该总单独的进程。

When you backgroud a process or execute it in a subshell (roughly equivalent), you can suspend that separate process in total.

这篇关于一个命令行循环内BG / FG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆