LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-03-2010, 08:11 PM   #1
markotitel
Member
 
Registered: Feb 2009
Location: Titel - Serbia
Posts: 181

Rep: Reputation: 18
I have commandline want to embed in a script


Hello,

Im having trouble inserting a command with output as shown
Quote:
[root@server test]# squidstat --host 10.20.64.14 --port 8080 | awk '/active IPs:/ {print $6}' | sed '$s/,$//'
11
[root@server test]#
Now i want thaat in a perl script i tried something like this
Quote:
#!/usr/bin/perl

open(PROCESS, "squidstat --host 10.20.64.14 --port 8080 | awk '/active IPs:/ {print $6}' | sed '$s/","$//' |");
$output = <PROCESS>;
close(PROCESS);
chomp($output);
print $output;
but it wont work .
I have no programing experience so any help is welcome
 
Old 05-03-2010, 08:30 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by markotitel View Post
Hello,

Im having trouble inserting a command with output as shown

Now i want thaat in a perl script i tried something like this


but it wont work .
I have no programing experience so any help is welcome
Start by putting

Code:
use strict;
use warnings;
just after

Code:
#!/usr/bin/perl
and make sure your script compiles and produces no warnings during run.
 
Old 05-03-2010, 08:48 PM   #3
markotitel
Member
 
Registered: Feb 2009
Location: Titel - Serbia
Posts: 181

Original Poster
Rep: Reputation: 18
Hello,

thanks for answer now I have this

Quote:
#!/usr/bin/perl
use strict;
use warnings;

open(PROCESS, "squidstat --host 10.20.64.14 --port 8080 | awk '/active IPs:/ {printf $6}' | sed '$s/,$//' |");
$output = <PROCESS>;
close(PROCESS);
chomp($output);
print $output;
And i get error like this

Quote:
Global symbol "$s" requires explicit package name at unix_processes.pl line 5.
Global symbol "$output" requires explicit package name at unix_processes.pl line 6.
Global symbol "$output" requires explicit package name at unix_processes.pl line 8.
Global symbol "$output" requires explicit package name at unix_processes.pl line 9.
 
Old 05-03-2010, 09:38 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by markotitel View Post
Hello,

thanks for answer now I have this



And i get error like this
So, fix the errors - read about global and lexical variables and about the way variables are interpolated in double quoted strings.

For example, start from visiting

http://perldoc.perl.org/search.html?q=lexical+variable
.
 
Old 05-04-2010, 07:06 AM   #5
markotitel
Member
 
Registered: Feb 2009
Location: Titel - Serbia
Posts: 181

Original Poster
Rep: Reputation: 18
Thanks ive got something like this that works maybe it is not by the rules but It serves my purposes
Quote:
#!/usr/bin/perl

my $sq = `squidstat --host 10.20.64.14 --port 8080`;
$sq = `echo "$sq" | awk '/active IPs:/'`;
$sq = `echo "$sq" | awk '\{printf \$6 \}'`;
$sq =~ s/,//;
chomp $sq;
print "users:$sq";
 
Old 05-04-2010, 08:02 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I must say I am curious why it takes two awk and a sed to get what you require??
Maybe if you show what squidstat returns you we could tidy that up? (only if you want of course )
 
Old 05-04-2010, 08:16 AM   #7
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Why do need awk inside Perl !!
Perl itself can do these stuff for you!!
 
Old 05-04-2010, 08:34 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@PMP - and that is definitely another point ...
 
Old 05-04-2010, 08:47 AM   #9
markotitel
Member
 
Registered: Feb 2009
Location: Titel - Serbia
Posts: 181

Original Poster
Rep: Reputation: 18
Ok thank you for interest in my question, here is output of squidstat

Quote:
[root@server scripts]# squidstat --host 10.20.64.14 --port 8080

Active connections: 56, active IPs: 22, average speed: 302.6Kb/s

IP: 10.20.64.211 (size: 290Mb 14Kb, speed: 98.1Kb/s)
http://data44.sevenload.com/p2/slcom...vwllljinnh.flv

IP: 10.20.64.20 (size: 104Mb 472Kb, speed: 45.1Kb/s)
188.27.67.47:443
http://2.channel40.facebook.com/x/34...p_1661507833=0
http://1.channel40.facebook.com/x/32...p_1661507833=0
http://3.channel44.facebook.com/x/10...000179569334=4
http://0.channel40.facebook.com/x/15...p_1661507833=0
Output is a little biger but what do I need is ACTIVE IPS value, I want to graph that value in cacti.
If you can show me nicer and cleaner way please help me.
 
Old 05-04-2010, 09:00 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
$sq=`echo $sq | awk -F: '/active/{gsub(/^[ ]*|,.*/,"",$3);print $3}'`
But as PMP said, Perl is one of the masters of regex (not me unfortunately) and can probably do this cleaner all in the same language.

I hope this helps though
 
Old 05-04-2010, 09:36 AM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
At all, when it's in Perl, no sed/awk/grep is needed.
 
Old 05-04-2010, 10:09 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@Sergei - I agree, perhaps you would show us the equivalent in Perl (as I have said I am not so familiar)
 
Old 05-04-2010, 10:23 AM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by grail View Post
@Sergei - I agree, perhaps you would show us the equivalent in Perl (as I have said I am not so familiar)
For that I would need to learn 'awk' in the first place .

...

If one needs to extract 22 from the line below:

Code:
Active connections: 56, active IPs: 22, average speed: 302.6Kb/s
, then in Perl one can write it this way:

Code:
my $stdout = `squidstat --host 10.20.64.14 --port 8080`;

if($stdout =~ m/\bActive\s+connections\:\s+\d+,\s+active\s+IPs\:\s+(\d+)/s)
  {
  my $number_of_active_connections = $1;
  warn "\$number_of_active_connections=$number_of_active_connections";
  }
else
  {
  warn "unexpected \$stdout=$stdout";
  }

- I haven't checked the code.
 
Old 05-04-2010, 01:12 PM   #14
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Quote:
Originally Posted by Sergei Steshenko View Post
For that I would need to learn 'awk' in the first place .

...

If one needs to extract 22 from the line below:

Code:
Active connections: 56, active IPs: 22, average speed: 302.6Kb/s
, then in Perl one can write it this way:

Code:
my $stdout = `squidstat --host 10.20.64.14 --port 8080`;

if($stdout =~ m/\bActive\s+connections\:\s+\d+,\s+active\s+IPs\:\s+(\d+)/s)
  {
  my $number_of_active_connections = $1;
  warn "\$number_of_active_connections=$number_of_active_connections";
  }
else
  {
  warn "unexpected \$stdout=$stdout";
  }

- I haven't checked the code.
Do we really need such a big pattern ?
only this should be sufficient, not tested though.

Code:
if ($stdout =~ /active\s+IPs\:\s+(\d+)/)
{
..
}
For this particular example line
Is it really required to use regex ?
I can see I can work with split only
 
Old 05-04-2010, 01:23 PM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by PMP View Post
Do we really need such a big pattern ?
only this should be sufficient, not tested though.

Code:
if ($stdout =~ /active\s+IPs\:\s+(\d+)/)
{
..
}
For this particular example line
Is it really required to use regex ?
I can see I can work with split only
First you need to identify the line, don't you ? So, how are you going to identify the line in the first place ?

Regarding the pattern - I wanted to identify the line as unambiguously as possible.

Last edited by Sergei Steshenko; 05-04-2010 at 01:24 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Could not embed Perl in C vinayashrestha Programming 3 02-18-2009 02:47 AM
commandline autocompletion for my shell script mmn357157 Linux - General 5 09-11-2008 11:29 AM
Embed X... okeyla Linux - Software 3 09-02-2005 03:34 PM
Encrypting Passwords in a commandline Script vendolis Linux - Newbie 1 08-29-2003 12:29 AM
Embed Console elist Linux - General 2 01-11-2002 06:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration