gpt4 book ai didi

linux - Expect脚本完成后不退出

转载 作者:太空宇宙 更新时间:2023-11-04 09:53:38 37 4
gpt4 key购买 nike

我有以下情况。

我的主要期望脚本看起来像这样

#!/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) 是一个非常大的脚本,可以为所需的环境做很多事情。其中之一就是运行守护进程和一些远程作业。这是一个非常简短的例子...

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 完成时,第一个 expect 脚本(通过生成进程调用它的脚本)不会退出。我必须打开新终端,然后用“kill -9”将其杀死。甚至无法发送 Ctrl+c。

编辑...如果我从两个 expect 脚本中删除“交互”,我将不会得到任何结果。这就是主脚本尾部的样子。

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 命令。

最佳答案

如果你想让第一个expect脚本结束,interact命令在最后做什么?您需要使用 interact 命令来检测 eof。

interact {
...
-o
eof {puts "spawned process completed"; exit}
}

-o 表示将从派生进程而不是用户检测以下规则(“eof”)。

关于linux - Expect脚本完成后不退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8153093/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com