如何使用期待与可选提示? [英] How to use expect with optional prompts?

查看:128
本文介绍了如何使用期待与可选提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我试图写有三个提示一个test.sh的期望脚本:PROMPT1,PROMPT2,prompt3

Let's say I am trying to write an expect script for a test.sh that has three prompts: prompt1, prompt2, prompt3.

我的code是这样的:

My code is like this:

spawn test.sh
expect "prompt1"
send "pass1"
expect "prompt2"
send "pass2"
expect "prompt3"
send "pass3"

不过,PROMPT2只出现一半的时间。如果PROMPT2不露面,预计脚本休息。我怎么会写期待code,它跳过PROMPT2如果它不显示?

However, prompt2 only occurs half the time. If prompt2 doesn't show up, the expect script breaks. How would I write expect code that skips over prompt2 if it doesn't show up?

编辑:

固定我的code:

/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

此方式,脚本的其余部分执行并提供输出

This way, the rest of the script executes and provides output.

推荐答案

您可以期待许多内容:

expect { 
    "prompt2" { 
        send "pass2"
        expect "prompt3"
        send "pass3"
    }
    "prompt3" {
        send "pass3"
    }
}

这篇关于如何使用期待与可选提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆