473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Executing Shell

Hi.
I need some help converting some php to perl.

$meminfo = shell_exec( "free -o" );
I need to execute "free-o" in shell and get the results.
It grabs memory information about a linux webserver.

If it is also possible to run a regular expression on this data that
would also be great.
It then outputs it in a nice format.

Here it is:

preg_match_all("/Mem:\s+?([0-9]+)\s+?([0-9]+)\s+?([0-9]+)\s+?([0-9]+)\s+?([0-9]+)\s+?([0-9]+).*?Swap:\s+?([0-9]+)\s+?([0-9]+)\s+?([0-9]+)/si",$meminfo,$mtype);

array_shift($mtype);

echo("<mem>"."\n");
echo("<total>".$mtype[0][0]."</total>"."\n");
echo("<used>".$mtype[1][0]."</used>"."\n");
echo("<free>".$mtype[2][0]."</free>"."\n");
echo("<shared>".$mtype[3][0]."</shared>"."\n");
echo("<buffers>".$mtype[4][0]."</buffers>"."\n");
echo("<cached>".$mtype[5][0]."</cached>"."\n");
echo("</mem>"."\n");
echo("<swap>"."\n");
echo("<total>".$mtype[6][0]."</total>"."\n");
echo("<used>".$mtype[7][0]."</used>"."\n");
echo("<free>".$mtype[8][0]."</free>"."\n");
echo("</swap>"."\n");
echo("</meminfo>"."\n");
If you can do either bit please give me an example of what I would
write.
(perl shell exec or perl regular expression)

This is the first line of my cgi script. Im not sure if I have to
declare headers or anything. Im new to perl.
This script outputs xml and as php is being turned off on my server I
must convert it to perl.

Thanks alot in advance.
William

Jul 19 '05 #1
1 4976
In article <11*********************@l41g2000cwc.googlegroups. com>,
speedster <co*************@hotmail.com> wrote:
Hi.
I need some help converting some php to perl.
Disclaimer: I do not know PHP.

$meminfo = shell_exec( "free -o" );
I need to execute "free-o" in shell and get the results.
It grabs memory information about a linux webserver.
my $meminfo = `free -o`;

If you expect more than one line, then you might want to put the output
into an array:

my @meminfo = `free -o`;

If it is also possible to run a regular expression on this data that
would also be great.
if( $meminfo =~ /regular expression/ ) {
# string in $meminfo matches regular expression
}

You don't say if you want to:

1) just know if it matches or not,
2) extract substrings, or
3) perform substitutions.
It then outputs it in a nice format.
Use print or printf.

Here it is:
preg_match_all("/Mem:\s+?([0-9]+)\s+?([0-9]+)\s+?([0-9]+)\s+?([0-9]+)\s+?([0-9]
+)\s+?([0-9]+).*?Swap:\s+?([0-9]+)\s+?([0-9]+)\s+?([0-9]+)/si",$meminfo,$mtype)
;
my @mtype = ( $meminfo =~ /.../ );

The regular expression (...) should be the same, although I would say
that you can use \d for [0-9], and using ? to set \s+ non-greedy is
useless (and ignored). The captured matches will also be stored in $1,
$2, $3, ...

array_shift($mtype);

echo("<mem>"."\n");
echo("<total>".$mtype[0][0]."</total>"."\n");
echo("<used>".$mtype[1][0]."</used>"."\n");
echo("<free>".$mtype[2][0]."</free>"."\n");
echo("<shared>".$mtype[3][0]."</shared>"."\n");
echo("<buffers>".$mtype[4][0]."</buffers>"."\n");
echo("<cached>".$mtype[5][0]."</cached>"."\n");
echo("</mem>"."\n");
echo("<swap>"."\n");
echo("<total>".$mtype[6][0]."</total>"."\n");
echo("<used>".$mtype[7][0]."</used>"."\n");
echo("<free>".$mtype[8][0]."</free>"."\n");
echo("</swap>"."\n");
echo("</meminfo>"."\n");
You should ensure that the regular expression matches before using the
captured results:

if( @mtype ) {

print "<mem>$mtype[0]\n" .
"<total>$mtype[1]</total>\n" .
"<used>$mtype[2]</used>\n" .
... etc.;
}


If you can do either bit please give me an example of what I would
write.
(perl shell exec or perl regular expression)

This is the first line of my cgi script. Im not sure if I have to
declare headers or anything. Im new to perl.
This script outputs xml and as php is being turned off on my server I
must convert it to perl.


Look into use of CGI.pm module that comes with most Perl distributions.
Most Perl distributions also come with on-line documentation that is
comprehensive and up-to-date, although sometimes finding things may be
difficult. Try:

perldoc perl
perldoc perlintro
perldoc perlre for regular expressions, etc.

This newsgroup is defunct. Try comp.lang.perl.misc in the future. You
should post real code there, and, since you don't know Perl, some
indication of what you are trying to do, not just "please convert this
PHP to Perl for me".

Good luck.
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Falk Schneider | last post by:
I wrote a PHP shell script under Linux which puts all existing PS-Files within a directory into a list and should then start a single Ghostview window for each file. Sounds simple, but it's not: ...
13
by: Darren Dale | last post by:
Some time ago I asked about executing a python program or script. For windows, I was informed that the .py extension could be added to some list of executable extensions, and then I could just type...
18
by: Brad Pears | last post by:
Can someone give me some sample code on how one would go about executing a command line "command" from within an ASP form? We need to run an application called GnuPG which allows us to encrypt an...
3
by: PzYon | last post by:
Hey 2gether! I'm trying to execute a PERL script on the web server when the user presses a button in an ASP.NET Web Application using Visual Basic. The most obvious solution for me seemed to be...
0
by: Bruceneedshelp | last post by:
My application starts logging and executes shell commands on startup. The application runs fine, except when it executes at boot time. In other words when I make it a "startup" application on my...
2
by: Mark | last post by:
hi, i've written a c++ program that converts one file type to another via two arguments (the input and output filenames). i want to execute this on my server, using something like ...
2
by: gagandutta01 | last post by:
Hi, Can anyone tell me how to execute a function declared in Oracle Package from Unix shell script? I created a shell script and after connecting to oracle database i am using exec @...
1
by: Svenn Are Bjerkem | last post by:
Hi, as a user on a linux system I am member of the groups "users" and "design" with users as my default group. To controll the accessibility of some parts of the file system, creation of files and...
2
by: ashutoshrawat | last post by:
Hi I had 2 AIX system.i want to run a shell script on one machine, which will execute another shell script on another AIX machne. the 2nd remotely executed shell script will execute a java...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.