imac
imac

Reputation: 95

Expect not matching

I had an expect script that was working; it contained the following lines:

expect "\[Y\]\> "
send "n\r"

The OS I'm interacting with changed, I now have a mixed environment of boxes. Some now have "[Y]>", some have "[1]>" at the point I'm trying to deal with.

I tried changing the code to:

expect {
        "\[Y\]\> " { send "n\r";exp_continue }
        "\[1\]\> " { send "2\r";exp_continue }
}

however, running in debug I see:

"Choose the password option:
1. Mask passwords 
2. Plain passphrases
[1]>
expect: does "s...ses\r\n[1]> " (spawn_id exp5) match glob pattern "[Y]> "? no 
"[1]> "? no
expect: timed out"

I don't understand why the revised code is not working, for either "[Y]> " or "[1]> ", when "[Y]> " was being matched before the 'else' statement was introduced.

Upvotes: 1

Views: 862

Answers (1)

Dinesh
Dinesh

Reputation: 16428

The problem is with escaping the square brackets which is special to Tcl.

# Using braces for pattern
expect {
    {\[Y]>} {puts Y}
    {\[1]>} { puts 1}
}
# or 
# Using double quotes
expect {
    "\\\[Y]>" {puts Y}
    "\\\[1]>" { puts 1}
}

We don't have to escape the closing bracket ] and the symbol >

Upvotes: 1

Related Questions