Timeout
正确写法:
当Expect有多个选项时,应用{}括起来
# 设置超时时间为20秒
set timeout 20
expect {
# 检测文件结尾
eof {puts "检测到文件结尾"
# 检测超时
timeout {puts "等待超时}
}
错误写法:
expect eof {puts "检测到文件结尾"}
timeout {puts "等待超时"}
[]的正确用法
正确写法:
expect "\[a-f]" {...}
or
expect {[a-f]} {...}
错误写法:
expect "[a-f]" {...}
错误提示:invalid command name “a-f”
【错误处理】couldn’t compile regular expression pattern: quantifier operand invalid
shell测试脚本
# shell demo test.sh
#!/usr/bin/bash
read -p "pls input phone number" number
echo $number > log.txt
expect测试脚本
# expect demo test.exp
#!/usr/bin/expect
spawn sh test.sh
expect -re "*phone*" {puts "match successfully"}
运行expect脚本
chmod a+x test.exp
./test.exp
报错
couldn’t compile regular expression pattern: quantifier operand invalid
代码修改
# expect demo test.exp
#!/usr/bin/expect
spawn sh test.sh
expect -re "\*phone\*" {puts "match successfully"}
将特殊字符用反斜线处理,程序正常运行,不在报错。