4

我在 perl 中使用 Expect 连接到远程机器并执行某些功能。示例代码就像

$outfile="ls -lrt";
$outfile1="output";

$exp->expect(30,-re,".*bash-.*" => sub{$exp->send("$outfile2 >$outfile \r")});
$exp->expect(60,-re,".*bash-.*" => sub{$exp->send("$shayam > $Ram \r")});

即使第一个表达式失败,它也会等待 60 秒并执行第二个语句。我只是想检查一下,如果只有第一个语句通过,它应该继续。

4

1 回答 1

4

我猜您正在使用此处记录的 Expect.pm 模块。如那里所述:

如果在数组上下文中调用,expect() 将返回($matched_pa​​ttern_position、$error、$successfully_matching_string、$before_match 和 $after_match)。

因此,您可能希望在数组上下文中调用它,以便在正则表达式失败和发送失败时都会收到错误消息。

my ($matched_pattern_position, $error,
  $successfully_matching_string,
  $before_match, $after_match) =
  $exp->expect(30
  , -re,".*bash-.*" =>
    sub{$exp->send("$outfile2 >$outfile \r")}
);

$exp->expect(60
  ,-re,".*bash-.*" =>
    sub{$exp->send("$shayam > $Ram \r")}
) if !defined $error;
于 2010-07-16T12:47:17.227 回答