【问题标题】:Expect -re not failing on eof期望 -re 不会在 eof 上失败
【发布时间】:2021-05-21 13:53:49
【问题描述】:

我有以下期望脚本。

这是test.exp

#!/usr/bin/expect

# exp_internal 1
# log_file -noappend ~/expect.log

# Use `send_log` to print to log file

set timeout 30

set bold [exec tput bold]
set red [exec tput setaf 1]
set green [exec tput setaf 2]
set normal [exec tput sgr0]

proc test_label {value} {
  upvar bold bold
  upvar normal normal
  puts "Running ${bold}${value}${normal}…"
}
proc test_send {value} {
  sleep 0.1
  send "$value"
}
proc test_failed {} {
  upvar bold bold
  upvar red red
  upvar normal normal
  sleep 0.1
  puts "${bold}${red}Failed${normal}"
  exit 1
}
proc test_ok {{force_close false}} {
  upvar bold bold
  upvar green green
  upvar normal normal
  sleep 0.1
  puts "${bold}${green}OK${normal}"
  if {$force_close} {
    close
  }
}

expect_before {
  default {
    test_failed
  }
}

这是electrum.exp

#!/usr/bin/expect

source ./test.exp

test_label "Should create Electrum mnemonic"

spawn qr-backup.sh --create-electrum-mnemonic

expect {
  -re {Format USB flash drive \(y or n\)\?} {
    test_send "n\r"
  }
}

expect {
  -re {\[sudo\] password for pi:} {
    test_send "$env(password)\r"
  }
}

expect {
  -re {Creating Electrum mnemonic…}
}

expect {
  -re {([a-z]+ ?){24}} {
    test_ok true
  }
}

spawn qr-backup.sh --create-electrum-mnemonic 返回的最后一行是electrum: error: unrecognized arguments: --nbits 264 时,为什么脚本不会失败?

【问题讨论】:

    标签: tcl expect


    【解决方案1】:

    想通了!

    使用eof 语句解决。

    expect {
      -re {([a-z]+ ?){24}} {
        test_ok true
      }
      eof {
        test_failed
      }
    }
    

    【讨论】:

    • 如果你想在很多地方做同样的事情,考虑使用expect_after { eof { test_failed }}
    • 感谢@DonalFellows 的提醒!
    • default 模式覆盖了eof。 Glenn jackman 解释了代码不起作用的真正原因。用expect_after {eof {test_failed}} 替换expect_before {default {test_failed}} 不会有任何区别。
    【解决方案2】:

    从期望的手册页中注意这一点:

    expect_before [expect_args]

    除非被-i 标志覆盖,否则expect_before 模式与在执行expect_before 命令时定义的生成ID 相匹配(而不是在其模式匹配时)。

    (强调我的)

    执行expect_before 命令时,没有激活的生成ID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 2019-05-25
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      • 2022-06-21
      • 2013-10-04
      相关资源
      最近更新 更多