我怎样才能在tcl中发送ctrl + c

问题描述 投票:1回答:1

有没有办法可以发送一个tcl程序的Ctrl + C信号?

我有一个tcl code,当我执行它时,在内部它应该通过Ctrl + C信号并打印如下:

将“发送ctrl + c”放在同一个文件中。

proc abc {                                 

         # Want to sent ctrl + c"
         Here I want the command for ctrl+c
         puts " sent ctrl+c"
}
tcl
1个回答
2
投票

如果您要将信号发送到Expect控制下的程序,您可以:

send "\003"

这就是当您执行Ctrl + C时键盘立即生成的字符;它被终端驱动程序转换成信号。

否则,您需要使用TclX软件包(或Expect,但如果您需要其全部功能,则只应使用它),它提供了kill命令:

package require Tclx

kill SIGINT $theProcessID
# You could also use INT or 15 to specify the signal to send.
# You can provide a list of PIDs instead of just one too.

了解要发送到哪个进程ID是在创建进程时跟踪事物的问题。如果你没有给它任何参数,那么pid命令会返回当前进程的PID。创建的子进程的进程ID由exec ... &返回,用于它创建的后台管道中的所有(已知)进程。对于使用open |...创建的管道,将管道的通道句柄传递给pid命令以获取子进程ID。

set pipeline [open |[list program1 ... | program2 ... | program3 ...] "r+"]
puts $pipeline "here is some input"
set outputLine [gets $pipeline]

kill SIGINT [pid $pipeline]

# This close *should* probably produce errors; you've killed the subprocesses after all
catch {close $pipeline}

如果您正在处理中断信号,请使用TclX中的signal命令执行此操作:

package require Tclx

signal error SIGINT;    # Generate a normal Tcl error on signal
signal trap SIGINT {;   # Custom signal handler
    puts "SIGNALLED!"
    exit
}
signal default SIGINT;  # Restore default behaviour

如果您使用signal error SIGINT,生成的错误将显示此消息“SIGINT signal received”,此错误代码为“POSIX SIG SIGINT”。这很容易测试(特别是使用Tcl 8.6的try … trap …命令)。

© www.soinside.com 2019 - 2024. All rights reserved.