tags:

views:

80

answers:

3

The Perl script that contains a Unix command to grep the ethernet NICs cannot be executed within the script! I have tried "qx" , $var and "system" but it does not seem to work!

The codes:

#!/usr/bin/perl

use warnings;
use strict;
use Term::ANSIColor;

print "\nYou are now in Showing Ethernet Cards!\n\n";

print "**************************\n";
print "|Ethernet Cards Available|\n";
print "**************************\n";

print "\nThe Ethernet Cards that are available are: "; 
my $ex = system ('ifconfig | awk '{print $1}' | egrep "eth|lo"');
print "$ex";

When executed the error "syntax error at ./ethercards.pl line 14, near "'ifconfig | awk '{" Execution of ./ethercards.pl aborted due to compilation errors." shows up in the terminal.

Does anyone have any thoughts on this? Thanks!

+3  A: 

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

system ('ifconfig | awk \'{print $1}\' | egrep "eth|lo"');
maat
Strange...Why is there an extra "0" poping up? when I run the command purely on the terminal it does not have the zero but when its being run in the script a "0" pops up. Any thoughts on that? [EDIT] Just take away the "my $ex = ".....
JavaNoob
The zero is the return value of your command. Zero usually means there was no error.
eumiro
So is there a way to remove the zero?
JavaNoob
Try the backtick operator instead. System itself prints to the stdout, and its return value is the exit code. Your print statement prints only the return value (the exit code). http://perldoc.perl.org/perlop.html#Quote-Like-Operators
maat
Awesome dude! The backtick idea works!
JavaNoob
+4  A: 

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. :)

brian d foy
Hi Brian, thanks for your reply! I know this code works but sometimes the directory changes therefore I need to run the command instead. Thanks for your answer though! + Rep!
JavaNoob
What does a directory have to do with this? This code is just the Perl version of your string of commands. There's no functional difference.
brian d foy
There is a restriction of root and non-root users.
JavaNoob
What does that have to do with anything?
brian d foy
A: 

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

my $ex = qx!ifconfig | awk '{print \$1}' | egrep "eth|lo"!;
print "$ex";
eumiro
Hi thanks for your reply! Works nicely! Sorry but first comes first serve....But up reps!
JavaNoob