On 08/04/2017 03:42 PM, Brandon Allbery wrote:
On Fri, Aug 4, 2017 at 6:28 PM, ToddAndMargo <toddandma...@zoho.com <mailto:toddandma...@zoho.com>> wrote:

    Here is my last attempt:

    ip -o -f inet addr show | perl6 -e 'for ( lines ) -> $I { if ( $I ~~
    /enp/ ) { say "Interface <" ~ .words[1] ~ ">\nIP<" ~ .words[3] ~
    ">"; } }'
    No such method 'words' for invocant of type 'Any'
       in block <unit> at -e line 1


Somewhere above I mentioned that the default for .words was $_. Not $I, which you have chosen to use. So you'll need to specify the explicit invocant:

ip -o -f inet addr show | perl6 -e 'for lines() -> $I { if ( $I ~~ /enp/ ) { say "Interface <" ~ $I.words[0] ~ ">\nIP<" ~ $I.words[3] ~ ">"; } }'

If I swap out your match for a prefix that exists on my system:

pyanfar Z$ ip -o -f inet addr show | perl6 -e 'for lines() -> $I { if ( $I ~~ /wlan/ ) { say "Interface <" ~ $I.words[1] ~ ">\nIP<" ~ $I.words[3].split("/")[0] ~ ">"; } }'
     Interface <wlan0>
     IP<10.8.201.162>

(For this one, I also removed the CIDR mask from the end.)


Yippee!

$ ip -o -f inet addr show | perl6 -e 'for lines() -> $Str { if ( $Str ~~ /enp/ ) { say "\n$Str\n\nInterface <" ~ $Str.words[1] ~ ">\nIP<" ~ $Str.words[3].split("/")[0] ~ ">\n" ~ "Short Mask <" ~ $Str.words[3].split("/")[1] ~ ">\n"; } }'

3: enp7s0 inet 192.168.250.135/24 brd 192.168.250.255 scope global dynamic enp7s0\ valid_lft 152704sec preferred_lft 152704sec

Interface <enp7s0>
IP<192.168.250.135>
Short Mask <24>

My big misunderstanding was not putting the variable name in front
of the .words.

Thank you!!!


As an exercise, I redid the thing as a program.
You guys may want my RunNoShell subroutine

Also at vpaste (no truncated lines):
http://vpaste.net/UFtOJ


<code>
#!/usr/bin/env perl6

my $ipStr;
my $ipRtnCode;
my $ipInterface;
my $ipIP;
my $ipShortMask;
my $ipLongMask;


sub RunNoShell ( $RunString) {
   #`{ run a system command without a shell.
Enter the command as a sting. Use quotes around items that are meant
       to be grouped together ("Program Files", etc.)

       Results are returned as a dual pair:
           a string with the results of the command, and
           the return code (note: bash 0=success)
   }

   # place each value into a cell in an array.  Keep quoted values together
   my @RunArray  = $RunString ~~ m:global/ [ '"' <-[ " ]> * '"' | \S+ ] /;

   # shells remove the quote, so you have to here as well
   for @RunArray.kv -> $index, $value { @RunArray[$index] ~~ s:g/\"//; };
# for @RunArray.kv -> $index, $value { say "\$RunArray[$index] = <$value>"; }; print "\n";

   my $proc      = run( @RunArray, :out );
   my $ReturnStr = $proc.out.slurp-rest;
   my $RtnCode   = $proc.status;

   return ( $ReturnStr, $RtnCode );
}


( $ipStr, $ipRtnCode ) = RunNoShell ( "ip -o -f inet addr show" );

for ( $ipStr.lines ) -> $Line { if ( $Line ~~ /enp/ ) { $ipStr = $Line; last; } }

$ipInterface = $ipStr.words[1];
$ipIP        = $ipStr.words[3].split("/")[0];
$ipShortMask = $ipStr.words[3].split("/")[1];
$ipLongMask  = $ipStr.words[5];

say "   Interface  = <$ipInterface>";
say "   IP         = <$ipIP>";
say "   Short Mask = <$ipShortMask>";
say "   Long Mask  = <$ipLongMask>\n";
</code>

$ ./GetNetwork.pl6
   Interface  = <enp7s0>
   IP         = <192.168.250.135>
   Short Mask = <24>
   Long Mask  = <192.168.250.255>

Reply via email to