stupid perl question.
John Chambers
jc at trillian.mit.edu
Sun Nov 11 10:19:55 EST 2001
Frank J.Ramsay asks:
| Why doesn't this work?
| What it's doing up dumping the output to the console and not assigning it to
| $ipaddy.
|
| #!/usr/bin/perl
Are you sure? I changed it slightly, to:
#!/usr/bin/perl
my $ipaddy = `/sbin/ifconfig | grep Bcast | sed \'s/:/ /g\' | awk \'{print $3}\'`;
chomp $ipaddy;
print "ipaddy=\"$ipaddy\"\n";
This puts some text around the value of $ipaddy so I can verify that
it comes from the perl script. What I get on my linux box is:
ipaddy=" inet addr 127.0.0.1 Bcast 127.255.255.255 Mask 255.0.0.0
inet addr 209.6.184.54 Bcast 255.255.255.255 Mask 255.255.252.0
inet addr 192.168.1.17 Bcast 192.168.1.255 Mask 255.255.255.0"
This makes it clear that the output of awk is ending up in $ipaddy.
What puzzles me is why awk gives the value that it does. But then,
awk has always been somewhat of a random tool. I'd wonder why you
would even bother with awk from a perl script.
Why not just assign the output of `/sbin/ifconfig | grep Bcast` to a
perl array, and do the rest of the job in perl? You'll save two
processes, and get a lot more control of the results. And if it
doesn't work, you'll have an interactive debugger to help figure out
what went wrong. (This latter point has long been one of the chief
arguments in favor of perl.)
More information about the Discuss
mailing list