Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Perl script - piping problem

SOLVED
Go to Solution
Super Advisor

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
1 ACCEPTED SOLUTIONS
Honored Contributor Honored Contributor

Re: Perl script - piping problem

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

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

MK
MK
10 REPLIES
Honored Contributor Honored Contributor

Re: Perl script - piping problem

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

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

MK
MK
Super Advisor

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
Acclaimed Contributor Acclaimed Contributor

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...
Super Advisor

Re: Perl script - piping problem

But ipcalc is a Liux utility.
Jesus is the King
Acclaimed Contributor Acclaimed Contributor

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...

Acclaimed Contributor Acclaimed Contributor

Re: Perl script - piping problem

Piotr:

Did this solve your problem?

...JRF...
Super Advisor

Re: Perl script - piping problem

Yes
Thanks
Jesus is the King
Super Advisor

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
Acclaimed Contributor Acclaimed Contributor

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...
Highlighted
Super Advisor

Re: Perl script - piping problem

closed
Jesus is the King