在下面的示例中,我尝试使用$expect_out(1,string)打印子模式组1;但是程序没有使用"$expect_out(1,string)“。相反,它打印出来(1,字符串)(参见下面的输出)。
任何关于如何打印子模式组1(即"SunOS")的想法。
我在搜索模式中插入括号,以指示组1以进行反向引用;内部调试(在输出中)似乎表明它正确地捕捉到了短语,但我不知道如何打印出来。
谢谢,
#!/usr/bin/perl
use Expect;
my $exp = new Expect;
# Begin: 2 lines for debugging only
$exp->exp_internal(1);
$exp->log_file("./expect_log.txt");
# End: 2 lines for debugging only
$exp->spawn("uname -a");
$exp->expect(10, '-re', "^(SunOS).*") or print "\nNot found!\n";
print "$expect_out(1, string)\n";输出:
unix-machine% test_expect.pl
SunOS unix-machine 5.12 Generic_000000-00 sun4u sparc SUNW,SPARC-Bus
(1, string)输出并进行内部调试:
86 unix-machine% test_expect.pl
Spawned 'uname -a'
spawn id(3)
Pid: 25613
Tty: /dev/pts/169
at /home/user/PERL/lib/Expect.pm line 181
Expect::spawn('Expect=GLOB(0x2b2040)', 'uname -a') called at test_expect.pl line 10
Starting EXPECT pattern matching...
at /home/user/PERL/lib/Expect.pm line 561
Expect::expect('Expect=GLOB(0x2b2040)', 10, '-re', '^(SunOS).*') called at test_expect.pl line 11
spawn id(3): list of patterns:
#1: -re `^(SunOS).*'产卵id(3):' match: pattern #1: -re^(SunOS).*'?不是的。
SunOS 5.10通用_000000-00 sun4u sparc SUNW,SPARC-Bus
产卵id(3):SunOS unix-machine 5.12 Generic_000000-00 sun4u sparc SUNW,SPARC-Bus\r\n' match: pattern #1: -re^(SunOS).*'?是的!!匹配前字符串:' Match string:SunOS unix-machine 5.12 sun4u _000000-00 sun4u sparc SUNW,SPARC-Bus\r‘后匹配字符串:\n' Matchlist: (SunOS') (1,string)
发布于 2013-01-12 09:34:44
对于快速和直接的解决方案,请使用函数$exp->matchlist。如果你愿意的话,你可以继续读一些细节。
" $expect_out (1,string)“适用于expect脚本,但不适用于Perl脚本,因为Perl解释器将$expect_out视为标量变量,而下面的"(1,字符串)”将作为原始字符串。有关细节,您可以参考Perl和PHP如何使用双引号中的字符串的规则。
有关如何使用Perl的Expect.pm模块的详细信息,请参阅CPAN:Expect.pm
输入此页面并搜索“括号”,您将知道$exp->matchlist函数将完成此任务。
因此,将脚本更改为as:
#!/usr/bin/perl
use Expect;
my $exp = new Expect;
## Begin: 2 lines for debugging only
$exp->exp_internal(1);
$exp->log_file("./expect_log.txt");
## End: 2 lines for debugging only
$exp->spawn("uname -a");
$exp->expect(10, '-re', "^(SunOS).*") or print "\nNot found!\n";
#print $expect_out(1, string);
print ${$exp->matchlist}[0],"\n";https://stackoverflow.com/questions/14291843
复制相似问题