if else语句在expect脚本中

时间:2016-01-10 20:51:09

标签: unix if-statement expect

我正在尝试创建一个脚本,它将发送"发送"根据执行上一个脚本收到的输出输入。

#!/usr/bin/expect --
set timeout 60

spawn ssh user@server1

expect "*assword*" { send "password\r"; }

expect "*$*" { send "./jboss.sh status \r"; }
if [ expect "*running*" ];
        then { send "echo running \r"; }
else { send "./jboss.sh start \r"; }
fi

我正在尝试做这样的事情,但坚持if else声明。请提出建议。谢谢!

1 个答案:

答案 0 :(得分:3)

您可以简单地将它们分组为单个expect语句,无论哪个匹配,都可以相应地进行处理。

#!/usr/bin/expect
set timeout 60
spawn ssh user@server1
expect "assword" { send "password\r"; }
# We escaped the `$` symbol with backslash to match literal '$' 
# The last '$' sign is to represent end-of-line
set prompt "#|%|>|\\\$ $"
expect {
        "(yes/no)"  {send "yes\r";exp_continue}
        "password:" {send "password\r";exp_continue}
        -re $prompt 
}
send "./jboss.sh status\r"
expect {
        "running" {send "echo running\r"}
        -re $prompt {send "./jboss.sh start \r"}
}
expect -re $prompt