What's the usage of `expect_before` command in Expect?

只愿长相守 提交于 2020-05-16 22:37:00

问题


The doc says:

takes the same arguments as expect, however it returns immediately.

But what does it mean by "returns immediately" ?

What's the usage of this command could be ?


回答1:


Image you have spawned a program that, say, randomly asks "Are you sure [yn]?"
Imagine this program has 100 questions that need to be answered.

You don't want to have to conditionally expect an "Are you sure" question for each of those 100 questions.

Expect lets you do:

spawn /some/annoying/program

expect_before {
    "Are you sure \[yn]?" {
        send "y\r"
        exp_continue
    }
}

expect "first question"
send "first answer\r"
# and so on.

Now you are covered: expect implicitly adds the expect_before code into each expect command.



来源:https://stackoverflow.com/questions/61021868/whats-the-usage-of-expect-before-command-in-expect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!