如何在tcl中搜索数字,即进程ID并终止进程ID [英] How to search a digit i.e process id in tcl and kill the process id

查看:32
本文介绍了如何在tcl中搜索数字,即进程ID并终止进程ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图搜索存储在变量中的进程 ID i-e 6762

<块引用>

nohup tcpdump -ni eth0 -s0 2>&1 </dev/null &[1] 6762您在/var/mail/root 中有新邮件

如果匹配,那么我想杀死它.

我尝试了以下代码:

foreach line [split $buffer "\n"]{if {[regexp {\[\d\]\s+(\d+)}$line 垃圾 pid]}休息}如果 {[信息存在 $pid]} {puts "nohup 的 PID 是 $pid"}

执行上述代码时出现以下错误

<前>错误 # args: 应该是foreach varList list ?varList list ...? command"执行时"foreach line [split $Buffer "\n"]{"(过程Test_SNMP_Trap"第 21 行)从内部调用Test_SNMP_Trap"(文件./SNMP_TRY.tcl"第 46 行)

如何搜索进程 ID 然后正确销毁它?

解决方案

几乎正确.您需要一个空格来分隔第一个和第二个参数.此外,我会将第一个 \d 更改为 \d+,因为您总是有可能拥有 9 个以上的后台作业.

if {[regexp {\[\d+\]\s+(\d+)} $line 垃圾 pid]}

还有 [info exists ...] 作用于一个变量,而不是一个值:

[信息存在pid]

添加最终代码片段示例

foreach 行中缺少空格.{ 之前需要有一个空格.并且没有附加 if 语句的主体.

Tcl 中的解析器与其他一些语言的工作方式不同.换行符和空格很重要.

所以最终的代码将如下所示:

foreach line [split $buffer "\n"] {if { [regexp {\[\d+\]\s+(\d+)} $line 垃圾 pid] } \休息}如果 { [信息存在 pid] } {puts "nohup 的 PID 是 $pid"}

if 语句也可以(更好):

if { [regexp {\[\d+\]\s+(\d+)} $line 垃圾 pid] } {休息}

I have tried to search process id i-e 6762 stored in a variable say buffer

nohup tcpdump -ni  eth0 -s0  2>&1 </dev/null &
[1] 6762
You have new mail in /var/mail/root

If it matches then I want to kill it.

I have tried the following code:

foreach line [split $buffer "\n"]{
    if {[regexp {\[\d\]\s+(\d+)}$line junk pid]}
       break
}

if {[info exists $pid]} {
    puts "PID of nohup is $pid"
}

Following error i am getting while executing the above code

wrong # args: should be "foreach varList list ?varList list ...? command"
    while executing
"foreach line [split $Buffer "\n"]{"
    (procedure "Test_SNMP_Trap" line 21)
    invoked from within
"Test_SNMP_Trap"
    (file "./SNMP_TRY.tcl" line 46)

How can i search a process id and then correctly destroy it?

解决方案

Almost right. You need a space to separate the first and second arguments. Also I would change the first \d to \d+, as there's always the possibility that you could have more than 9 background jobs.

if {[regexp {\[\d+\]\s+(\d+)} $line junk pid]}

Also [info exists ...] acts on a variable, not a value:

[info exists pid]

Edit: Add example of final code snippet

There is a missing space in the foreach line. There needs to be a space before the {. And the body of the if statement was not attached.

The parser in Tcl doesn't work in the same manner as some other languages. Line continuations and spaces are important.

So the final code will look like:

foreach line [split $buffer "\n"] {
    if { [regexp {\[\d+\]\s+(\d+)} $line junk pid] } \
       break
}

if { [info exists pid] } {
    puts "PID of nohup is $pid"
}

The if statement could also be (better):

if { [regexp {\[\d+\]\s+(\d+)} $line junk pid] } {
   break
}

这篇关于如何在tcl中搜索数字,即进程ID并终止进程ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆