[英]Why can't I retrieve my program that was suspended from inside backticks?
I created a program that takes a list of arguments and put them in a grid on a new tty where I can move around and select from it what I want.我创建了一个程序,它接受一个参数列表,并将它们放在一个新 tty 上的网格中,我可以在其中移动并从中选择我想要的。
When I run the program without backticks like this...当我运行程序时没有像这样的反引号......
$> ./ft_select arg_1 arg_2 ... arg_N
A new tty is opened and a grid is shown...打开一个新的 tty 并显示一个网格...
arg_1 arg_2 arg_3
arg_4 arg_5 arg_6
arg_7 ... arg_N
I hit ctrl+z and the program gets suspended with no problem and fg
command puts it back.我按ctrl+z并且程序被暂停,没有问题,
fg
命令将其放回原处。
My problem is that when I put the command between backticks and I try to suspend it, it get stuck without giving the prompt back.我的问题是,当我将命令放在反引号之间并尝试挂起它时,它会卡住而不返回提示。
I have to mention that I write all the content of the grid on /dev/tty
不得不提一下,我把grid的所有内容都写在
/dev/tty
You can find in the code below the function that does the signal handling.您可以在下面的代码中找到执行信号处理的函数。
23 void signalhandler(int sig)
24 {
25 // struct winsize ws;
26
27 if (sig == SIGWINCH)
28 {
29 // ioctl(g_data->tty, TIOCGWINSZ, &ws);
30 update_data(g_data);
31 print_args(g_data);
32 update_cursor(g_data, 1);
33 }
34 else if (sig == SIGTSTP)
35 {
36 signal(SIGTSTP, SIG_DFL);
37 enable_cap("te");
38 modify_main_caps(SET, g_data->tty);
39 ioctl(g_data->tty, TIOCSTI, "\032");
40 }
41 else if (sig == SIGCONT)
42 {
43 signal(SIGTSTP, signalhandler);
44 modify_main_caps(UNSET, g_data->tty);
45 update_data(g_data);
46 print_args(g_data);
47 update_cursor(g_data, 1);
48 }
49 else if (sig == SIGINT)
50 {
51 enable_cap("te");
52 modify_main_caps(SET, g_data->tty);
53 exit(EXIT_FAILURE);
54 }
55 }
CTRL + Z causes the terminal device driver to send a SIGTSTP
to all processes in the foreground process group. CTRL + Z使终端设备驱动程序
SIGTSTP
进程组中的所有进程发送SIGTSTP
。 In bash
and zsh
, at least, commands in a command substitution $(<command>)
are executed in a subshell, but they don't get their own process group (they run in the same process group as the parent shell itself).至少在
bash
和zsh
,命令替换$(<command>)
中的命令在子shell中执行,但它们没有自己的进程组(它们与父shell本身在同一进程组中运行)。 That means that CTRL + Z should not affect them at all - press it and nothing happens, just like pressing CTRL + Z in a shell prompt doesn't do anything: in both cases, there is no foreground process group to suspend!这意味着CTRL + Z根本不应该影响它们 - 按下它并没有任何反应,就像在 shell 提示中按下CTRL + Z不会做任何事情一样:在这两种情况下,都没有要挂起的前台进程组!
I think you're looking at a variation of the issue discussed in this question .我认为你在寻找在讨论这个问题的变化这一问题。
Putting a command in backquotes isn't just another way to run it;将命令放在反引号中不仅仅是运行它的另一种方式; it tells the shell to run that command in a subshell and substitute the results.
它告诉 shell 在子 shell 中运行该命令并替换结果。 This is called command substitution , and can also be represented as
$(command)
(this syntax is more modern and usually preferred ).这称为命令替换,也可以表示为
$(command)
(这种语法更现代,通常更受欢迎)。
The main use of command susbtitution is that it lets you use the output of one command as the input to another.命令 susbtitution 的主要用途是它允许您使用一个命令的输出作为另一个命令的输入。 For example, look at the results of these commands:
例如,查看这些命令的结果:
echo "1 + 1 = $(expr 1 + 1)" # prints "1 + 1 = 2"
echo "2 + 2 = `expr 2 + 2`" # prints "2 + 2 = 4"
echo "1 + (4 / 2) = $(expr 1 + $(expr 4 / 2))" # prints "1 + (4 / 2) = 3"
When you use command substitution in place of the command itself, the the expansion still works, attempting to run the command and include its output in the shell's input.当您使用命令替换代替命令本身时,扩展仍然有效,尝试运行命令并将其输出包含在 shell 的输入中。 So when you run your command in a substitution, it tries to run your command, taking all of its input, then should try to run whatever it prints as the result.
因此,当您在替换中运行您的命令时,它会尝试运行您的命令,获取其所有输入,然后应尝试运行它作为结果打印的任何内容。
I'm not exactly sure what happens when you try to suspend these, but my testing gave some unusual behaviors: trying to suspending $(cat)
did nothing, but suspending $(rlwrap cat)
crashed, and left the terminal confused after I killed it.我不确定当你尝试挂起这些时会发生什么,但我的测试给出了一些不寻常的行为:尝试挂起
$(cat)
什么也没做,但是挂起$(rlwrap cat)
崩溃了,并且在我杀死后让终端感到困惑它。 But I do know that subshells aren't managed along with actual shell jobs, so you won't be able to manage a command that you run in a subshell.但是我知道子shell 不与实际的shell 作业一起管理,因此您将无法管理在子shell 中运行的命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.