首页 > 解决方案 > 期望脚本调用 bash 脚本,输出可能不会发生

问题描述

我正在尝试编写一个调用 bash 脚本的期望脚本,该脚本可能会也可能不会输出一行文本:

...
expect "this string appears"
...
expect "this string may or may not appear"
...
expect "this string appears"
...

如果出现“这个字符串可能出现也可能不出现”,或者如果没有出现,我该如何编写代码来发送一些东西?

标签: bashexpect

解决方案


您将使用多模式期望语句:

expect {
    "this string may or may not appear" {
        # do stuff ...
        exp_continue
    }
    "this string appears"
}
# ... do more stuff

exp_continue命令本质上是在 expect 命令中“循环”,因此它可以继续寻找任何给定的模式。最后一个模式“这个字符串出现”没有动作块,所以expect命令返回并且脚本继续执行下一个命令。


推荐阅读