Operating System - HP-UX
1748058 Members
5688 Online
108758 Solutions
New Discussion юеВ

Re: Perl script - piping problem

 
SOLVED
Go to solution
Piotr Kirklewski
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
10 REPLIES 10
Matti_Kurkela
Honored Contributor
Solution

Re: Perl script - piping problem

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

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

MK
MK
Piotr Kirklewski
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
James R. Ferguson
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...
Piotr Kirklewski
Super Advisor

Re: Perl script - piping problem

But ipcalc is a Liux utility.
Jesus is the King
James R. Ferguson
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...

James R. Ferguson
Acclaimed Contributor

Re: Perl script - piping problem

Piotr:

Did this solve your problem?

...JRF...
Piotr Kirklewski
Super Advisor

Re: Perl script - piping problem

Yes
Thanks
Jesus is the King
Piotr Kirklewski
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
James R. Ferguson
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...