我有一个Expect脚本,在该脚本中我并不总是知道输出,也不想硬编码从给定命令返回的预期输出,而是将预期输出的一部分作为命令行变量传递。然后,我想将预期的输出作为正则表达式的一部分进行验证。
我看了几个示例,只能看到send var与send命令一起使用,而没有与Expect -re命令一起使用。
#!/usr/bin/expect
set output [lindex $argv 0]
set host [lindex $argv 1]
set regex "{(?n)^${output}.*}"
puts $regex
set timeout -1
spawn ssh root@$host
expect -re {(?n)^Password.*}
send "myPassword\n"
expect -re $regex
expect eof
输出
Password: couldn't compile regular expression pattern: quantifier operand invalid
while executing
"expect -re $regex"
预期 正则表达式应作为在输出cli变量中设置的值进行处理。
请注意:当对值进行硬编码时,脚本将按预期工作
答案 0 :(得分:0)
正则表达式中的括号是量词语法。例如,要找到3个“ a”,可以使用a{3}
您对$ regex变量的分配包含大括号
set regex "{(?n)^${output}.*}"
expect是Tcl语言的扩展:在Tcl中,使用双引号将“单词”括起来,以允许变量和命令替换;您使用花括号将一个单词括起来,不允许任何替换(例如外壳中的单引号)。
只需从$ regex值中删除括号。