簡體   English   中英

來自全局的循環列表

[英]loop list from global

我有一個ftp服務器列表。 這總是可以改變的

列出:

set ftp1 "192.168.0.12 -u test,test"
set ftp2 "192.168.0.13 -u test,test"
set ftp3 "192.168.0.14 -u test,test"

這是一個tcl代碼,我想在tcl中查看從列表到exec的所有ftp,而不是依次執行,而是相乘

set ftp1 "192.168.0.12 -u test,test"
set ftp2 "192.168.0.13 -u test,test"
set ftp3 "192.168.0.14 -u test,test"

proc search {nick host handle channel text} {
    global ftp1 ftp2 ftp3
    set text [stripcodes bcru $text]
    set searchtext [lindex [split $text] 0];
    set ftp1 "192.168.0.12 -u test,test"
    set results [exec sh f.sh $ftp1 $searchtext]
    foreach elem $results {
        putnow "PRIVMSG$channel :ftp1 $elem"
    }
}

最簡單的事情是編寫另外一些幫助程序。 這些過程應該搜索一個站點,然后通過回調將結果返回給您的代碼(因為我們在這里討論異步處理)。

# This is a fairly standard pattern for how to do async reading from a pipeline
# Only the arguments to [open |[list ...]] can be considered custom...

proc searchOneHost {hostinfo term callback} {
    set pipeline [open |[list sh f.sh $hostinfo $term]]
    fconfigure $pipeline -blocking 0
    fileevent $pipeline readable [list searchResultHandler $pipeline $callback]
}
proc searchResultHandler {pipeline callback} {
    if {[gets $pipeline line] >= 0} {
        uplevel "#0" [list {*}$callback $line]
    } elseif {[eof $pipeline]} {
        close $pipeline
    }
}

# The rest of this code is modelled on your existing code

set ftp1 "192.168.0.12 -u test,test"
set ftp2 "192.168.0.13 -u test,test"
set ftp3 "192.168.0.14 -u test,test"

proc search {nick host handle channel text} {
    set searchtext [lindex [split [stripcodes bcru $text]] 0]
    foreach v {ftp1 ftp2 ftp3} {
        upvar "#0" $v ftp
        searchOneHost $ftp $searchtext [list report $channel $v]
    }
}
proc report {channel name found} {
    foreach elem $found {
        putnow "PRIVMSG$channel :$name $elem"
    }
}

我僅引用#0來替代熒光筆。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM