如何使用一系列参数调用期望脚本

How can I call an expect script with a sequence of arguments

我有一个 expect 脚本,如下所示:

#!/usr/bin/expect
set path_start [lindex $argv 0]
set host [lindex $argv 1]
spawn ssh root@$host telnet jpaxdp
expect {\-> }

set fh [open ${path_start}${host} r]
while {[gets $fh line] != -1} {
    send "$line\r"
    expect {\-> }
}
close $fh

send "exit\r"
expect eof

我将其命名为 ./script.sh cmds_ cc1,现在我的主机编号为 1 - 8,我尝试将脚本命名为 ./script cmds_ cc[1-8],但由于脚本解释 [=20] 而无法正常工作=]host[1-8] 作为参数并向我展示:

spawn ssh root@cc[1-8] telnet jpaxdp
ssh: Could not resolve hostname cc[1-8]: Name or service not known
couldn't open "cmds_cc[1-8]": no such file or directory
    while executing
"open ${path_start}${host} r"
    invoked from within
"set fh [open ${path_start}${host} r]"
    (file "./script.sh" line 7)

我怎样才能使这个工作?

cc[1-8] 是文件名通配符,它​​查找与该模式匹配的文件。如果没有,则通配符本身将保留在参数列表中。要获取数字范围,请使用 cc{1..8}。而要重复运行这个命令,你需要一个for循环。

for host in cc{1..8}
do
    ./script.sh cmds_ "$host"
done