linux - 如何将 expect 与可选提示一起使用?

标签 linux bash expect

假设我正在尝试为具有三个提示的 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/

相关文章:

c - 使用 sscanf 解析十进制字符串

php - file_get_contents 通过 tor

bash - 使用 kdialog bash 脚本创建动态选项菜单 : Printf can't do the job ? 引用问题

linux - 极客中的原始转换器

linux - 特定进程的iotop

bash - 为什么进程替换在 shell 脚本中不起作用?

bash - 来自 CRON 的 AWS SES sendmail 失败

linux - 如何在linux expect的exec命令中执行多个命令

regex - linux期待命令

authentication - Telnet 自动化与 Expect : Slow authentication?