我写了这个脚本:
#!/bin/sh
expect -c "
spawn cu -l /dev/ttyUSB0 -s 115200
expect \"foo\"
send \"bar\"
"
echo $?
我运行它时没有/dev/ttyUSB0
:
$ ls /dev/ttyUSB0
ls: cannot access '/dev/ttyUSB0': No such file or directory
$ ./test.sh
spawn cu -l /dev/ttyUSB0 -s 115200
cu: open (/dev/ttyUSB0): No such file or directory
cu: /dev/ttyUSB0: Line in use
send: spawn id exp4 not open
while executing
"send "bar""
0
为何expect
退货0
?
如何检测spawn
故障?
答案1
你可以测试foo
和 eof
#!/bin/sh
expect <<'END_EXPECT'
spawn cu -l /dev/ttyUSB0 -s 115200
expect {
eof {
puts stderr "Cannot open cu"
exit 1
}
"foo"
}
send "bar"
END_EXPECT
echo $?
我发现使用此处文档比使用带引号的字符串来包含期望代码更容易。这里引用的文档意味着您\$expect_variables
也不必担心转义。