почему ожидаемый не может соответствовать `[0-9]`? - PullRequest
0 голосов
/ 29 мая 2019

Из следующего скрипта мы видим, что простое число не может быть сопоставлено с [0-9], но в документе regexp сказано, что оно поддерживает это.Почему моя версия не работает?

#!/bin/expect
set timeout 20
set remote_cmd_prompt_regex "> $"
spawn bash --noprofile --norc
send "PS1='> '\r"
expect {
    -regexp "$remote_cmd_prompt_regex" {
        puts "Host ->$expect_out(buffer)"
        send "echo 99\r"
        # match with .* works
        expect -regexp ".*\\n(.+)\\n$remote_cmd_prompt_regex"
        puts "regex group ->$expect_out(1,string)"
        send "echo 89\r"
        # match with [0-9] does not work why?
        expect -regexp ".*\\n(\[0-9]+)\\n$remote_cmd_prompt_regex"
        puts "regex group ->$expect_out(1,string)"
    }
    timeout {
    puts "Timeout error!"
    exit 1
    }
}

1 Ответ

1 голос
/ 30 мая 2019

Обычно (в каноническом режиме pty) каждый \n, отправленный на pty, преобразуется в \r\n, поэтому вам нужно изменить \n на \r\n в шаблоне RE. Пример:

[STEP 101] # cat foo.exp
#!/usr/bin/expect

set ps_re "> $"
spawn -noecho bash --noprofile --norc

expect bash
send "PS1='> '\r"
expect -re $ps_re

for { set i 10 } { $i < 15 } { incr i } {
    send "echo $i\r"
    expect -re ".*\\r\\n(\[0-9]+)\\r\\n$ps_re"
}

send "exit\r"
expect eof
[STEP 102] # expect foo.exp
bash-5.0# PS1='> '
> echo 10
10
> echo 11
11
> echo 12
12
> echo 13
13
> echo 14
14
> exit
exit
[STEP 103] #
...