假设我正在尝试为具有三个提示的 test.sh 编写 expect 脚本:prompt1、prompt2、prompt3。
我的代码是这样的:
spawn test.sh
expect "prompt1"
send "pass1"
expect "prompt2"
send "pass2"
expect "prompt3"
send "pass3"
然而,prompt2 只出现了一半的时间。如果 prompt2 没有出现,expect 脚本就会中断。如果 prompt2 没有出现,我将如何编写跳过 prompt2 的 expect 代码?
编辑:
修复了我的代码:
/usr/bin/expect -c '
spawn ./test.sh
expect {
"prompt1" {
send "pass1\r"
exp_continue
}
"prompt2" {
send "pass2\r"
exp_continue
}
"prompt3" {
send "pass3\r"
exp_continue
}
}
interact return
'
这样,脚本的其余部分将执行并提供输出。
最佳答案
只要你有一个总是被期望命中的案例并且在这种情况下不包含 exp_continue
,你就可以删除重复并轻松处理可选提示:
expect "prompt1"
send "pass1"
expect {
"prompt2" {
send "pass2"
exp_continue
}
"prompt3" {
send "pass3"
}
}
关于linux - 如何将 expect 与可选提示一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17621050/