【问题标题】:Expect script output at Perl console期望 Perl 控制台上的脚本输出
【发布时间】:2015-02-26 03:37:40
【问题描述】:

我有一个 exp 脚本 -script.exp 来输入名称和密码。它调用 test.sh 文件,如下所示。

set timeout -1
spawn ./test.sh -create
match_max 100000
expect -exact "Enter the name: "
send -- "abcd\r"
expect -exact "\r
Please confirm password: "
send -- "xxy\r"
expect -exact "\r
expect eof

它在期望窗口给我一个输出 - “成功输入姓名” 如果出现问题,它会引发异常或错误消息。

我在 Perl 文件中运行这个期望脚本——如下所示

$cmd="expect script.exp";
system($cmd);
$outputfromexp=?

运行脚本后,我需要在 Perl 控制台中输出 exp 的失败或通过状态

我该怎么做?请帮帮我。

我尝试按照我的 Perl 脚本中的建议进行调用--

use strict;
use warnings;
    sub sysrun {
            my ($command) ="expect script.exp";
            my $ret_code;

            $ret_code = system("$command");
            if ( $ret_code == 0 ) {
                    # Job suceeded
                    $ret_code = 1;
            }
            else {
                    # Job Failed
                    $ret_code = 0;
            }
            return ($ret_code);
    }

    my $ret_code=sysrun();

    print "reuturmsfg- $ret_code\n";

但它什么也没打印——只是 reuturmsfg-。

我做了更改-

my $ret_code=sysrun();

它给了我 0 和 1 的返回码。

【问题讨论】:

  • Always use strictuse warningsevery Perl 程序的顶部。 从不使用 & 符号 & 调用 Perl 子例程。
  • 我使用了严格和警告。并删除 & 即使 $ret_code 没有输出

标签: perl expect


【解决方案1】:

如果我对问题的理解正确,得到你想要做的返回码:

system($cmd);
my $ret_code = $?;

从执行中获取 STDOUT 输出:

my @out = `$cmd`;
my $ret_code = $?;

我强烈建议在您的代码中使用严格和警告。

【讨论】:

  • 感谢您查看此 Pavel,我更新了我的问题。根据您的建议,请调查并帮助我..
  • 我使用了我的 $ret_code = system($cmd) 但没有输出。请帮助
  • 嗯。想了解为什么我的回答被否决了吗?
  • @Pavel- 我得到成功代码 -1。哇。谢谢。谢谢,
  • 绿色答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-13
相关资源
最近更新 更多