用于从ip命令获取inet的Perl脚本

用于从ip命令获取inet的Perl脚本,perl,Perl,我试图使用ip命令获取inet,它在cmd提示符下运行良好,但如果我将其添加到perl脚本中,它将无法按预期执行。脚本如下:- ip.pl use strict; my $a = `ip -f inet addr show eth0| grep -Po 'inet \K[\d.]+'`; chomp($a); print $a; 使用“perl a.pl”执行上面的命令只返回“ip-f inet addr show eth0 | grep-Po'inet\K[\d.]+'”返回inet值。

我试图使用ip命令获取inet,它在cmd提示符下运行良好,但如果我将其添加到perl脚本中,它将无法按预期执行。脚本如下:-

ip.pl

use strict;
my $a = `ip -f inet addr show eth0| grep -Po 'inet \K[\d.]+'`;
chomp($a);
print $a;
使用“perl a.pl”执行上面的命令只返回“ip-f inet addr show eth0 | grep-Po'inet\K[\d.]+'”返回inet值。 如何使用perl脚本执行它?

打开以获得提示:

Unrecognized escape \K passed through at ./1.pl line 5.
Unrecognized escape \d passed through at ./1.pl line 5.
反斜杠内的单引号不嵌套,需要反斜杠:

my $a = `ip -f inet addr show eth0| grep -Po 'inet \\K[\\d.]+'`;
对词法变量使用
$a
是错误的,当以后使用
$a
作为特殊变量时,可能会导致奇怪的错误。使用更有意义的名称

此外,通常不需要从Perl调用
grep
,您可以匹配Perl本身中的字符串:

my ($ip) = `ip -f inet addr show eth0` =~ /inet ([\d.]+)/;


perl inet.pl无法识别的转义\K在inet.pl第3行传递。无法识别的转义\d在inet.pl第3行传递。请尝试
my$a=qx'ip-f inet addr show eth0 | grep-Po“inet\K[\d.]+”
my ($ip) = `ip -f inet addr show eth0` =~ /inet \K[\d.]+/g;