在 Expect 脚本中读取变量行

Read lines for variables in Expect Script

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

第 1 行是 $user

第 2 行是 $password

第 3 行是 $logs1

第 4 行是 $logs2

第 5 行是 $containerName

我看过这个 link:Read file into String and do a loop in Expect Script 发现他们使用了

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

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

在您的示例中,hosts 是一个项目列表。您可以使用 lindex 命令访问各个项目。示例:

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

最直接的方法就是:

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