Perl script - piping problem (177 Views)
Reply
Super Advisor
Piotr Kirklewski
Posts: 339
Registered: ‎01-12-2007
Message 1 of 11 (177 Views)
Accepted Solution

Perl script - piping problem

Hi there

my @args = ( "ipcalc ", $net, "|","grep Wildcard" );
system (@args);

Why am i getting this:

ipcalc 2.1.2.0/20
|grep Wildcard

Rather then this:

ipcalc 2.1.2.0/20 |grep Wildcard

?
Jesus is the King
Please use plain text.
Honored Contributor
Matti_Kurkela
Posts: 6,271
Registered: ‎12-02-2001
Message 2 of 11 (177 Views)

Re: Perl script - piping problem

Your $net variable probably contains a newline character at the end.

Use "chomp ($net);" to remove it.

MK
MK
Please use plain text.
Super Advisor
Piotr Kirklewski
Posts: 339
Registered: ‎01-12-2007
Message 3 of 11 (177 Views)

Re: Perl script - piping problem

Yes it was the new line character. Thanks
Now what should I do to get rid of the error below ?

#!/usr/bin/perl

open (MYFILE,'/ip/a.txt');
while (){
$net = "$_"; #$net = "2.24.0.0/13"

chomp($net);
my @args = ( "ipcalc ", $net, "|","grep Wildcard|awk '{print \$2}' \>> /ip/b.txt" );
$cmd = @args[0].@args[1].@args[2].@args[3];
system ($cmd);
print "$cmd\n";
}
close (MYFILE);


Use of uninitialized value $ENV{"TERM"} in pattern match (m//) at /usr/bin/ipcalc line 714.
ipcalc 83.223.192.0/19|grep Wildcard|awk '{print $2}' >> /ip/b.txt
Jesus is the King
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 11 (177 Views)

Re: Perl script - piping problem

Hi:

> Now what should I do to get rid of the error below ?

Use of uninitialized value $ENV{"TERM"} in pattern match (m//) at /usr/bin/ipcalc line 714.
ipcalc 83.223.192.0/19|grep Wildcard|awk '{print $2}' >> /ip/b.txt

The environmental 'TERM' variable isn't set. Either set it before you run your script, or (better) test for "defined-ness" before referencing it. This will need to be done in '/ipcalc' --- the code for which you don't show.

Regards!

...JRF...
Please use plain text.
Super Advisor
Piotr Kirklewski
Posts: 339
Registered: ‎01-12-2007
Message 5 of 11 (177 Views)

Re: Perl script - piping problem

But ipcalc is a Liux utility.
Jesus is the King
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 11 (177 Views)

Re: Perl script - piping problem

Hi (again):

> But ipcalc is a Linux utility.

Then set the TERM variable to something in your script or on the command line!

# TERM=vt100 ./Piotr_Script

...Notice that there is whitespace after the setting without any semicolon before the run of your script. That sets the environmental variable only for the duration of your script.

Otherwise in your script do something like this shows:

# perl -le 'local $ENV{TERM}='vt100';print "[$ENV{TERM}]"'

Regards!

...JRF...

Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 7 of 11 (177 Views)

Re: Perl script - piping problem

Piotr:

Did this solve your problem?

...JRF...
Please use plain text.
Super Advisor
Piotr Kirklewski
Posts: 339
Registered: ‎01-12-2007
Message 8 of 11 (177 Views)

Re: Perl script - piping problem

Yes
Thanks
Jesus is the King
Please use plain text.
Super Advisor
Piotr Kirklewski
Posts: 339
Registered: ‎01-12-2007
Message 9 of 11 (177 Views)

Re: Perl script - piping problem

One more question.
How do I get the output of $cmd to a perl variable.

$var = $cmd doesn't work.

#!/usr/bin/perl
system ("TERM=vt100");
local $ENV{TERM}='vt100';
#print "[$ENV{TERM}]"';
open (MYFILE,'/ip/a.txt');
while (){
$net = "$_"; #$net = "2.24.0.0/13"

chomp($net);
my @args = ( "ipcalc ", $net, "|","grep Wildcard|awk '{print \$2}' \>> /ip/b.txt" );
$cmd = @args[0].@args[1].@args[2].@args[3];
system ($cmd);
}
close (MYFILE);
Jesus is the King
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 10 of 11 (177 Views)

Re: Perl script - piping problem

Hi (again) Piotr:

> How do I get the output of $cmd to a perl variable.

The simplest way, when you don't care about the return code is to use backticks:

# perl -le '$result=`uname -a`;print $result'

If you dislike the backticks use 'qx( .... )':

# perl -le 'perl -le '$result=qx(uname -a);print $result'

Regards!

...JRF...
Please use plain text.
Super Advisor
Piotr Kirklewski
Posts: 339
Registered: ‎01-12-2007
Message 11 of 11 (177 Views)

Re: Perl script - piping problem

closed
Jesus is the King
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation