bash Echo Control C 字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6196321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:35:01  来源:igfitidea点击:

Echo Control C character

bash

提问by qwerty

I need to grep the output of a third party program. This program dumps out data but does not terminate without pressing ^c to terminate it.

我需要 grep 第三方程序的输出。该程序会转储数据,但不会在不按 ^c 终止它的情况下终止。

I am currently searching and killing it using its pid. However, I was wondering however if it were possible to echo the control C character. Pseudo code would look like

我目前正在使用它的 pid 搜索并杀死它。但是,我想知道是否可以回显控制 C 字符。伪代码看起来像

echo ^c | ./program_that_does_not_terminate

I am able to do something like this in DOS, so there must be a way in linux.

我可以在 DOS 中做这样的事情,所以在 linux 中一定有办法。

C:\>echo y | del C:\tmp\*
C:\tmp\*, Are you sure (Y/N)? y
C:\>

回答by paxdiablo

No, piping a CTRL-C character into your process won't work, because a CTRL-C keystroke is captured by the terminaland translated into a kill signal sent to the process.

不,将 CTRL-C 字符通过管道传输到您的进程中是行不通的,因为终端会捕获 CTRL-C 击键并将其转换为发送到进程的终止信号。

The correct character code (in ASCII) for CTRL-C is code number 3 but, if you echo that to your program, it will simply receive the character from its standard input. It won't cause the process to terminate.

CTRL-C 的正确字符代码(在 ASCII 中)是代码编号 3,但是,如果您将其回显到您的程序,它只会从其标准输入接收字符。它不会导致进程终止。

If you wanted to ease the task of finding and killing the process, you could use something like:

如果您想简化查找和终止进程的任务,您可以使用以下内容:

./program_that_does_not_terminate &
pid=$!
sleep 20
kill ${pid}

$!will give you the process ID for the last started background process.

$!将为您提供上次启动的后台进程的进程 ID。

回答by jwodder

In Bash, a ^Ccan be passed as an argument on the command-line (and thus passed to echofor echoing) by writing $'\cc'. Thus, the desired command is:

在 Bash 中, a^C可以echo通过编写$'\cc'. 因此,所需的命令是:

echo $'\cc' | ./program_that_does_not_terminate

回答by alf-man

Try the following:

请尝试以下操作:

expect -c "send ##代码##3;"

回答by grundprinzip

I think, you will need something more complex here. If you want to capture the output of the program you can't send the kill signal immediately to the program. Compared to your example above, the delcommand waits for input, while CTRL+Cis no direct input but a kill signal.

我认为,您在这里需要更复杂的东西。如果要捕获程序的输出,则不能立即向程序发送终止信号。与上面的示例相比,该del命令等待输入,而CTRL+C不是直接输入,而是一个终止信号。

I would suggest a solution where you identify if all output is captured and send the kill signal afterwards. Hoever, the point in time when all output can only be determined by you.

我会建议一个解决方案,您可以确定是否捕获了所有输出并随后发送终止信号。但是,所有输出的时间点只能由您决定。

回答by Levi Wu

As @paxdiablo said, CTRL-C keystroke is captured by the terminal and translated into a kill signal sent to the process. So if your process is attached to a terminal, you can send $'\cc' provided by @jwodder to that terminal, letting the terminal send actual kill signal to the process.

正如@paxdiablo 所说,CTRL-C 击键被终端捕获并转换为发送到进程的终止信号。因此,如果您的进程附加到终端,您可以将@jwodder 提供的 $'\cc' 发送到该终端,让终端向该进程发送实际的终止信号。

Step:

步:

1.Compile the C file in Execute a command in another terminal via /dev/pts

1.通过/dev/pts在另一个终端执行命令编译C文件

2.sudo ./a.out -n /dev/pts/0 $'\cc'# change /dev/pts/0 to the tty your process attached to

2. sudo ./a.out -n /dev/pts/0 $'\cc'# 将 /dev/pts/0 更改为您的进程附加到的 tty