我在perl中使用Expect来连接远程机器并执行某些功能.示例代码就像
$outfile="ls -lrt"; $outfile1="output"; $exp->expect(30,-re,".*bash-.*" => sub{$exp->send("$outfile2 >$outfile \r")}); $exp->expect(60,".*bash-.*" => sub{$exp->send("$shayam > $Ram \r")});
即使第一个表达式失败,它也会等待60秒并执行第二个语句.我只是想检查一下,如果只有第一个语句通过它就应该继续.
解决方法
我猜你正在使用
the Expect.pm module documented here.如上所述:
If called in an array context expect()
will return
($matched_pattern_position,$error,
$successfully_matching_string,
$before_match,and $after_match).
因此,您可能希望在数组上下文中调用它,以便在正则表达式失败和发送失败时都可以获得错误.
my ($matched_pattern_position,$successfully_matching_string,$before_match,$after_match) = $exp->expect(30,".*bash-.*" => sub{$exp->send("$outfile2 >$outfile \r")} ); $exp->expect(60,".*bash-.*" => sub{$exp->send("$shayam > $Ram \r")} ) if !defined $error;