代码之家  ›  专栏  ›  技术社区  ›  E tom

读取Expect脚本中变量的行

  •  2
  • E tom  · 技术社区  · 6 年前

    我正在尝试编写一个expect脚本(这是第一次),我正在尝试从文本文件(input.txt)中读取行,并将它们指定为expect脚本中的特定变量。我的文本文件从一个web应用程序获取输入,将有5行(我的脚本有5个变量)。因为我已经知道每一行代表什么,所以我想为它们创建专门命名的变量。。

    第1行是$user

    第二行是$password

    第三行是$logs1

    第四行是$logs2

    第5行是$containerName

    我查看了以下链接: Read file into String and do a loop in Expect Script 并确保他们使用

        set f [open "host.txt"]
        set hosts [split [read $f] "\n"]
        close $f
    

    从一个包含所有主机名的文件中收集数据,并在循环中对它们进行迭代,但如何根据每一行所代表的内容对每一行进行不同的命名?

    2 回复  |  直到 6 年前
        1
  •  2
  •   Colin Macleod    6 年前

    最简单的方法就是:

    set f [open "host.txt"]
    gets $f user
    gets $f password
    gets $f logs1
    gets $f logs2
    gets $f containerName
    close $f
    

    有关更多背景信息,请参阅 https://www.tcl.tk/man/tcl8.5/tutorial/Tcl24.html

        2
  •  1
  •   user1934428    6 年前

    在你的例子中, hosts 是一个项目列表。您可以使用 lindex 命令例子:

    set user [lindex $hosts 0]
    set passwd [lindex $hosts 1]