我有以下情况。
我的主要期望脚本看起来像这样
#!/usr/bin/expect -d
set timeout 900
spawn main_ksh_script.ksh [lindex $argv 0]
expect {
"Press ENTER to Continue" {
send_user "Message1\n"
send "\r"
exp_continue}
"Press any key to continue" {
send "\r"
exp_continue}
eof {
send "\003\177\015"
send_user "EOF reached\n"
exit 0}
timeout {
send_user "Job timed out\n"
send "\003\177\015"
exit 100}
}
interact {
\015 {send "\015"}
\003 {send "\003\177\015"}
"\033\[3~" {send "\177"}
}
我的主要 korn shell脚本(main_ksh_script.ksh)是一个非常庞大的脚本,它可以为所需的环境做很多事情。其中一件事就是运行deamons和一些远程工作。这是一个非常简短的例子......
while true; do
clear
echo "\n\tDo You Want to deploy xxx!!! ([Yes]/No) \c"
read yn1 ;
case ${yn1} in
[Yy]*|$enter)
clear
echo "SomeJobName1" on Environment (${envNum})\n"
secondScript.expect environment$envNum@host "Some command(s)"
echo "End of SomeJobName1" on Environment (${envNum})\n"
echo "SomeJobName2" on Environment (${envNum})\n"
secondScript.expect environment$envNum@host "Some command(s)"
echo "End of SomeJobName2" on Environment (${envNum})\n"
#And so on...
break ;;
[Nn]*) break ;;
*) echo "\n\tPlease choose Yes or No\c"; pau ;;
esac
pau
done
secondScript.expect看起来像这样......
#!/usr/bin/expect -d
set timeout 300
spawn ssh -o PubkeyAuthentication=no [lindex $argv 0] -n [lindex $argv 1]
expect {
"Operational Job ended successfully" {
send "\003\177\015"
exp_continue}
"Operational Job ended with failure" {
send "\003\177\015"
exp_continue}
"Press ENTER to Continue" {
send "\r"
exp_continue}
"Press any key to continue" {
send "\r"
exp_continue}
"Password:" {
send "mypassword\r"
exp_continue}
eof {
send "\003\177\015"
send_user "EOF reached\n"
exit 0}
timeout {
send "\003\177\015"
exit 100}
}
interact
问题是,当main_ksh_script.ksh完成时,第一个期望脚本(通过生成进程调用它的脚本)不会退出。我必须打开新终端,并通过“kill -9”杀死它。甚至无法发送Ctrl + c。
编辑... 如果我从两个期望脚本中删除“交互”,我没有得到任何结果。这就是主脚本尾部的样子。
expect: does " \c\r\n\r\n" (spawn_id exp6) match glob pattern "Press ENTER to Continue"? no
"Press any key to continue"? no
expect: read eof
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) " \c\r\n\r\n"
它仍然不会自行终止,我仍然不能通过任何其他方式杀死它,期望来自另一个终端的kill命令。
答案 0 :(得分:2)
如果您希望第一个期望脚本结束,那么interact
命令最后会做什么?
您需要使用interact
命令来检测eof。
interact {
...
-o
eof {puts "spawned process completed"; exit}
}
-o
表示要从生成的进程中检测以下规则(“eof”),而不是来自用户。