Why is my Perl system command giving me a syntax error?

懵懂的女人 提交于 2019-12-07 03:24:29

Syntax highlighting also suggests that your system string is broken. Try

system ('ifconfig | awk \'{print $1}\' | egrep "eth|lo"');

You are using the ' as the string delimiter but the ' also shows up in the string.

You then mistake the return value of system as the output of the command. When a command doesn't do what you expect, read its docs.

You're also doing a bit too much work on the command line. You're already in Perl, so avoid creating extra processes when you don't need to:

my @interfaces = `/sbin/ifconfig` =~ m/^(\w+):/gm;

print "interfaces are @interfaces\n";

If you only want some interfaces, throw a grep in there:

my @interfaces = grep { /^(?:eth|lo)/ } `/sbin/ifconfig` =~ m/^(\w+):/gm;

print "interfaces are @interfaces\n";

I like to use the full path to executables so I know which one I'm getting. :)

If you need the output of your program, then write:

my $ex = qx!ifconfig | awk '{print \$1}' | egrep "eth|lo"!;
print "$ex";
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!