我有一个看起来像这样的期望脚本:
#!/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]
那样调用脚本,但是当脚本解释为时,它不起作用主持人[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)
我该如何做到这一点?
答案 0 :(得分:3)
cc[1-8]
是文件名通配符,它查找与该模式匹配的文件。如果没有,则通配符本身保存在参数列表中。要获取一系列数字,请使用cc{1..8}
。要重复运行该命令,您需要for
循环。
for host in cc{1..8}
do
./script.sh cmds_ "$host"
done