期望脚本无法识别“--MORE--”,而不是 运行 之后的任何命令

Expect script not recognizing "--MORE--", not running any commands after that

我正在编写 expect 脚本来自动执行 Sophos 安装。这是它的样子:

#!/usr/bin/expect

set installdir [lindex $argv 0]
set timeout 20

spawn "./install.sh"

expect {
    sleep 5
    "Press <return> to display Licence. Then press <spc> to scroll forward." {send "\r"}
    -ex "--More--" {send -- " "; exp_continue}
    "Do you accept the licence? Yes(Y)/No(N)\\[N\\]" {send "Y"}
    "Where do you want to install Sophos Anti-Virus? \\[/opt/sophos-av\\]" {"send $installdir/sophos-av"}
    "Do you want to enable on-access scanning? Yes(Y)/No(N) \\[Y\\]" {"send \r"}
    "Do you want to enable remote management? Yes(Y)/No(N) \\[Y\\]" {"send \r"}
    "Username for Sophos Anti-Virus GUI? \\[admin\\]" {"send \r"}
    "Password for Sophos Anti-Virus GUI?" {"********"}
    "Re-enter the same password." {"********"}
}

interact

当我 运行 expect 脚本时,它输入许可证的击键,但是当第一个“--MORE--”提示出现时什么都不做,并且 运行 没有后续命令。想法?想法?

您是否希望程序依次查看这些模式中的每一个?如果是这样,您需要使用多个顺序期望和发送命令:

sleep 5
expect "Press <return> to display Licence. Then press <spc> to scroll forward." 
send "\r"
expect {
    -ex "--More--" {send -- " "; exp_continue}
    "Do you accept the licence? Yes(Y)/No(N)\\[N\\]" 
}
send "Y"
expect "Where do you want to install Sophos Anti-Virus? \\[/opt/sophos-av\\]" 
send "$installdir/sophos-av\r"
expect "Do you want to enable on-access scanning? Yes(Y)/No(N) \\[Y\\]" 
send "\r"
expect "Do you want to enable remote management? Yes(Y)/No(N) \\[Y\\]" 
send "\r"
expect "Username for Sophos Anti-Virus GUI? \\[admin\\]" 
send "\r"
expect "Password for Sophos Anti-Virus GUI?" 
send "********\r"
expect "Re-enter the same password." 
send "********\r"
interact

一旦 expect 执行了 "action" 块,expect 命令将 return。它不会等待匹配其他模式,除非你告诉它 (exp_continue)

有用提示:在开发 expect 脚本时,打开详细调试输出:exp_internal 1 靠近脚本顶部。