在编译和执行一个涉及scanf()的C程序时,我遇到了一个有趣的问题。我使用Ubuntu20.04LTS与Bash和GCC v10.2.0。
#include <stdio.h>
int main(void)
{
int decimalInteger;
printf("Enter a decimal integer value: ");
scanf("%d", &decimalInteger);
printf("It can also be written in octal and hexadecimal notations as %o and %x, respectively.\nWith C prefixes, they are %#o (for octal) and %#x/%#X (for hexadecimal).\n", decimalInteger, decimalInteger, decimalInteger, decimalInteger, decimalInteger);
return 0;
}当我用gcc-10 *.c -std=c11 && ./a.out编译和运行它时,它运行得非常好。输入后按enter键后,光标移动到下一行。
使用完整命令输出:

但是,当我将bind -x '"\C-h":gcc-10 *.c -std=c11 && ./a.out'添加到.bashrc,然后使用Ctrl+H编译和执行程序时,输出如下所示:

控制台不会显示输入,光标也不会移动到下一行。
为什么会发生这种情况?
发布于 2020-12-28 06:02:40
这是正常的,readline在读取输入时更改终端设置。否则行编辑是不可能的。
您需要将原始终端设置保存到变量中,并在运行程序之前恢复它们。
stty_orig=$(stty -g)
my_func() {
local stty_bkup=$(stty -g)
stty "$stty_orig"
# compile and run here
stty "$stty_bkup"
}
bind -x '"\C-h": my_func'https://stackoverflow.com/questions/65465973
复制相似问题