If statement 如何在Tcl for telnet中使用带有expect的if语句?

If statement 如何在Tcl for telnet中使用带有expect的if语句?,if-statement,tcl,expect,telnet,control-flow,If Statement,Tcl,Expect,Telnet,Control Flow,自动telnet会话获取天气: thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ tclsh main.tcl connect to wunderground with: ----------------- 1) noControlFlow 2) connect 1 spawn telnet rainmaker.wundergrou

自动
telnet
会话获取天气:

thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ tclsh main.tcl 


connect to wunderground with:
-----------------
1)  noControlFlow
2)  connect


1
spawn telnet rainmaker.wunderground.com
getting weather for nyc
Trying 35.160.169.47...
Connected to rainmaker.wunderground.com.
Escape character is '^]'.
------------------------------------------------------------------------------
*               Welcome to THE WEATHER UNDERGROUND telnet service!            *
------------------------------------------------------------------------------
*                                                                            *
*   National Weather Service information provided by Alden Electronics, Inc. *
*    and updated each minute as reports come in over our data feed.          *
*                                                                            *
*   **Note: If you cannot get past this opening screen, you must use a       *
*   different version of the "telnet" program--some of the ones for IBM      *
*   compatible PC's have a bug that prevents proper connection.              *
*                                                                            *
*           comments: jmasters@wunderground.com                              *
------------------------------------------------------------------------------

Press Return to continue:

Press Return for menu
or enter 3 letter forecast city code-- nyc
Weather Conditions at 03:51 AM EDT on 10 May 2020 for New York JFK, NY.
Temp(F)    Humidity(%)    Wind(mph)    Pressure(in)    Weather
========================================================================
  40          55%         WEST at 20       30.08      Partly Cloudy

Forecast for New York, NY
319 am EDT sun may 10 2020

.Today...Sunny. Highs in the lower 60s. West winds 15 to 20 mph. 
.Tonight...Mostly cloudy. A chance of showers after midnight.
Lows in the upper 40s. Southwest winds 15 to 20 mph, diminishing
to 5 to 10 mph after midnight. Chance of rain 30 percent. 
.Monday...Mostly cloudy with a chance of showers. A slight chance
of thunderstorms in the afternoon. Highs in the lower 60s. West
winds 10 to 15 mph. Chance of rain 50 percent. 
.Monday night...Mostly cloudy with a slight chance of showers in
the evening, then mostly clear after midnight. Lows in the lower
40s. Northwest winds 15 to 20 mph. Chance of rain 20 percent. 
.Tuesday...Sunny. Highs in the upper 50s. Northwest winds 10 to
15 mph. 
.Tuesday night...Partly cloudy. Lows in the lower 40s. 
.Wednesday...Sunny. Highs in the lower 60s. 
.Wednesday night...Mostly clear in the evening, then becoming
   Press Return to continue, M to return to menu, X to exit: thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
代码:

如何向上述脚本添加一点逻辑或控制流

例如,要从
proc
本身打印几个硬编码城市,并显示一个菜单从中进行数字选择

类似于
main
脚本:

lappend auto_path /home/thufir/NetBeansProjects/spawnTelnet/telnet/weather
package require weather 1.0


puts "\n\nconnect to wunderground with:"
puts "-----------------"
puts "1)\tnoControlFlow"
puts "2)\tconnect\n\n"


set a [gets stdin]


if {$a == 1 } {
   wunderground::noControlFlow "nyc"
} else {
   wunderground::connect "nyc"
}
是的,很明显,wunderground提供了一个可供选择的城市菜单。只是想添加一些分支以供启发

我相信,通过
-I
打开
expect
语句来查看


我已将expect的
man
页面发送到我的kindle,并考虑在expect上购买这本书。

您可以在expect命令中使用正常的Tcl控制流结构,如
if
foreach
。(您已经使用
proc
)唯一真正棘手的一点(尤其是循环和期望)是,您需要考虑您正在做什么,以及正在进行的自动化工作

你最终得到的可能是这样的

proc ::wunderground::multipleCities {args} {
    variable telnet [spawn telnet rainmaker.wunderground.com]
    puts "getting weather for $city"

    expect "Press Return to continue:"
    send "\r"

    foreach city $args {
        expect "or enter 3 letter forecast city code--"
        send "$city\r"
    }

    expect "X to exit"
    send "x"
}

除此之外,我认为您可能应该以某种方式构造循环中的
expect
位于
send
之后,这样您就可以在概念上处理获取特定城市的值的结果。您可能需要确定一个常见的稳定提示,以便在获取数据后返回。

您也可以。如果您需要更多的帮助,您需要说明您想要做什么。例如,您可能对在
expect
命令中使用多个模式感兴趣;这通常可以代替在expect脚本中使用
if
,至少在匹配这个或那个或那个时是这样。这只是一个个人项目,可以执行以下操作:
proc ::wunderground::multipleCities {args} {
    variable telnet [spawn telnet rainmaker.wunderground.com]
    puts "getting weather for $city"

    expect "Press Return to continue:"
    send "\r"

    foreach city $args {
        expect "or enter 3 letter forecast city code--"
        send "$city\r"
    }

    expect "X to exit"
    send "x"
}