Hi All,
This is the result of the command:
$ ip -f inet addr show br0
4: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue
state UNKNOWN
inet
192.168.255.10/24 brd 192.168.255.255 scope global br0
In bash, I would extract the network and mask with
$ ip -f inet addr show br0 | grep inet | awk {'print $2}' | awk
-F "/" '{print $1}'
192.168.255.10
$ ip -f inet addr show br0 | grep inet | awk {'print $2}' | awk -F
"/" '{print $2}'
24
This is what I did in Perl.
$ more
ip.extract.pl
<code>
#!/usr/bin/perl
use strict;
use warnings;
my $Sysip=qx(ip -f inet addr show br0);
my $Start=index($Sysip, "inet ") + 5;
my $Stop=index($Sysip, " ", $Start);
my $Length= $Stop - $Start;
my $IpMask=substr $Sysip, $Start, $Length;
my ($ip, $mask) = split ('/', $IpMask);
print "ip = $ip\nmask = $mask\n";
<code>
I realize that I could have written
my $IpMask=substr $Sysip, $Start, $Length;
as
my $IpMask=substr $Sysip, $Start, $Stop - $Start;
But it is obvious what I am doing if I use $Length.
I also could have put $Start and $Stop into the
"substr", but that would have been an eyeful to
maintain later.
And I dropped the grep (Perl has one) as only one line
had "addr " on it.
Am I still thinking in Bash, or am I starting to transition?
Many thanks,
-T