2013-04-18 63 views
0

我想达到一定的期望脚本我有一个txt文件..需要与assitance expect脚本

它包含诸如

1 ip telnetname slot1 slot2 assoc1 assoc2 mep1 mep2 
2 ip telnetname slot1 slot2 assoc1 assoc2 mep1 mep2 

数据我想打一个期望脚本,每行在文件中它产生了一个telnet会话,其中的变量从第1行的spawn telnet中设置为变量,其中每一个字作为变量设置为稍后在命令中使用。

这是我试过到目前为止

foreach ip $ips telnetname $telnetnames slot1 $slots1 slot2 $slots2 { commands here } 
+0

缺少的是你尝试过的结果。因此,您确实需要帮助的问题。否则,这个问题看起来像可悲的标准“给我完整的代码”类型。 – kostix

+0

你可以编辑自己的答案,并尝试阐述你试图达到的目标吗?我不知道我明白你想要做什么。你的意思是你想要使用,例如,只有第一行而不是其他行,当你运行命令? (并且它可能只是另一次只有第3行) – Jerry

回答

0

这不是很清楚你想要什么,从你的描述的事,但它可能比较容易使你的代码的工作是这样的:

# Slurp the data into Tcl; it's not *that* long, is it? 
set f [open "inputfile.txt"] 
set data [read $f] 
close $f 

foreach line [split $data "\n"] { 
    # Tcl's [scan] is like C's sscanf(), but memory-safe! 
    if {[scan $line "%d %s %s %s %s" -> ip telnetname slot1 slot2] != 5} { 
     # Not what was expected; skip it! 
     continue 
    } 
    # Now do something with $ip, $telnetname, $slot1 and $slot2 ... 
} 
+0

另外,' - >'实际上是一个(奇怪的)变量名,我正在使用,因为我并不关心在开始时的数字线。 (假设这是一个真实的数字。) –