我有这样的代码:
#!/opt/tools/unsupported/expect-5.39/bin/expect
set timeout 10
match_max 256
log_file report.txt
expect_after eof {exit 0}
spawn ssh -l user ip
expect "(yes/no)?" { send "yes\r" }
expect "password:" { send "password\r" }
expect "~]#" { send "date\r" }
set timeout 600
expect "~]#" { send "scp user@ip:/sometlink/AMM.tar.gz /somelink/\r" }
expect "Password:" { send "password\r" }
sleep 5
set timeout 3600
expect "~]#" { send "swadm install import AMM\r" }
sleep 5
expect "~]#" { send "swadm install apply AMM\r" }
expect "~]#" { send "swadm install show AMM\r"}
expect -re "(.*)\r\nproduct-state=(.*)"
foreach line [split $expect_out(1,string) \n] {
if {[string match *Applied* $line]} {
send_log "Product install of AMM ----------------------------- Passed"
}
else {
send_log "Product install of AMM ----------------------------- Failed"
}
}
expect "~]#" { send "date\r" }
expect "~]#" { send "exit\r" }
它应该检查 swadm install show AMM 的输出
product-id=AMM
product-name=Application Manager
product-version=1.0.0
product-state=Applied
product-description=
NCL-list=AMMLV010
如果找到“应用”一词,则将其添加到测试文件中,表明安装成功,如果没有找到,则表明安装失败。但它给了我其他部分的错误
invalid command name "else"
while executing
"else {
send_log "Product install of AMM ----------------------------- Failed"
("foreach" body line 6)
invoked from within
"foreach line [split $expect_out(1,string) \n] {
if {[string match *Applied* $line]} {
send_log "Product install of AMM-----..."
答案1
我会这样做:
expect "~]#" { send "swadm install show AMM\r"}
expect "~]#" {
if {[string match {*product-state=Applied*} $expect_out(buffer)]} {
do-thing-1
} else {
do-thing-2
}
}
在“show”命令之后,只需等待下一个提示即可。然后,所期望的所有内容都在 中expect_out(buffer)
,并且您可以在整个缓冲区文本上使用字符串匹配(或正则表达式匹配,如果您愿意)。您不需要按行分割它。
答案2
我明白了这个问题!它应该是 } else { 之间没有新行。