Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

expect

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 ?

like image 991
yaquawa Avatar asked Oct 19 '25 18:10

yaquawa


1 Answers

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.

like image 88
glenn jackman Avatar answered Oct 26 '25 06:10

glenn jackman