c - 如何检测用户输入是 while 循环中的 [ Control ] + [ D ]

标签 c shell terminal controls exit

如何检测用户是否按下 [control + D]

我正在写一个shell。 shell 必须始终打印“> ”。
我正在尝试执行一个新命令 -> [control + d]
当用户按下[control]按钮[+]和[D]按钮
shell 应该退出

这是 sudo 代码

int user_press_control_D = 0;
while(user_press_control_D == 0){
  running();
  detect(user_input);
  if user_input == (Control + D){
  user_press_control_D = 1; //quit 
  }
}

如何实际写入/检测/检查用户按下(Control + D)

最佳答案

Trap 命令应该可以完成这项工作。

调整代码以捕获 EOF(ctrl+d) 并捕获信号

trap '回显“按下 ctrl+d”' 0 陷阱''2

读取数据时;做 echo “做好你的工作” 完成

关于c - 如何检测用户输入是 while 循环中的 [ Control ] + [ D ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58251791/

相关文章:

c - TCP Socket多路复用发送大数据

C: Exec/fork > 失效进程

linux - 想要从 Java 调用然后循环 linux shell 命令

Python:字典值中的八进制转义字符\033 在打印语句中转换为 UTF-8 字符

清除多行

c - 在使用 strcmp 针对两个可能值测试字符串的几种给定方法中,其中一种是显而易见的选择吗?

shell -/usr/bin/env 有什么作用?

Bash IFS 忽略行尾的分隔符

command-line - 安装oAuth PECL错误: Cannot install, php_dir for channel "pecl.php.net"is not writeable by current user

c - 在 C 中调用函数时的序列点和未定义/未指定的行为