我正在编写一个引导Linux映像的perl脚本,需要检测该映像是否到达登录提示符。我正在使用Device :: serial模块与开发板通信。我在检测登录字符串时遇到问题。我认为这可能与在Linux启动期间发生的大量打印有关。下面的代码尝试捕获提示。奇怪的是,只有当我添加了不必要的“读取”命令时,它才有效
# Wait for login prompt
$port->are_match("login:");
$gotit = "";
$timeout = 60;
until (($gotit ne "") or ($timeout eq 0))
{
$gotit = $port->lookfor; # poll until data ready
die "Aborted without match\n" unless (defined $gotit);
$read = $port->read(1000000);
$port->lookclear;
sleep(1);
$timeout--;
}
“ lookfor”是否完全适合Linux启动方案?为什么“读取”使此代码有效?
感谢大家
最佳答案
CPAN doc page说要这样做:
my $gotit = "";
until ("" ne $gotit) {
$gotit = $PortObj->lookfor; # poll until data ready
die "Aborted without match\n" unless (defined $gotit);
sleep 1; # polling sample time
}
他们的循环中没有对
$port->lookClear
的调用。我怀疑这助长了您的问题。还要注意该超时。关于linux - 使用Perl和串行设备检测Linux启动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10394247/