Search code examples
matchexpect

expect: How to cancel `expect_before` and `expect_after`?


It seems that using expect_before or expect_after once, they are applied to any expect following, causing unexpected timeouts.

So I wonder: How can I cancel a preceding expect_before or expect_after?

What I had tried so far without success (it still seems to match " ") was:

expect_after
expect_after { }

Or did I misunderstand the concept completely?

Trying to control a shell-like program, this is what I did (roughly):

  • As the program echoes its input ($cmd), use expect_before $cmd
  • As the program will output a prompt ($prompt) after the command, use expect_after $prompt
  • Use expect to match the actual command output

However there are two exceptions:

  1. When asking for a password, there won't be $prompt when expecting password input; thus I want to cancel the expect_after. As the password isn't echoed, I want to cancel the expect_before when sending the password, too.
  2. When quitting the program, there won't be a prompt after executing the quit command, so I want to cancel the expect_after.

Solution

  • I think you're misunderstanding Tcl braces. They are not special syntax, they are just a quoting mechanism -- they're like shell single quotes: no interpolation within. So expect_before { } means you're expecting a single space, and no action is specified.

    I think, to cancel an expect before, you use expect_before with no arguments.

    Use expect_before -info to see the current status.


    To ignore the sent $cmd, I think you want

    expect_before -ex $cmd {exp_continue}
    

    You need to redo this every time the cmd variable changes.