The following Perl example is part of long Perl script.
This script takes the results from ifconfig -a
and prints the IP address.
Can someone explain how $1
gets the IP address?
And what the regular expression
$RESULTS =~ /addr:(\S+)\s+/
means?
my $COMMAND = "ifconfig -a | grep inet | grep -v 127.0.0.1 | head -1";
my $RESULTS = `$COMMAND`;
chomp $RESULTS;
# inet addr:106.13.4.9 Bcast:106.13.4.255 Mask:255.255.255.0
# inet 106.13.4.9 netmask ffffff80 broadcast 106.13.4.127
if ( $RESULTS =~ /addr:(\S+)\s+/ ) {
$IpAddress = $1;
}
elsif ( $RESULTS =~ /inet\s+(\S+)\s+/ ) {
$IpAddress = $1;
}
print "IpAddress = $IpAddress\n";