473,563 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,$m type);

array_shift($mt ype);

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 4986
In article <11************ *********@l41g2 000cwc.googlegr oups.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,$m type)
;
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($mt ype);

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>$mtyp e[0]\n" .
"<total>$mt ype[1]</total>\n" .
"<used>$mty pe[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
10902
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: The following code provides for about half of the functionality I wanted: ==============code starts $bla = array(); foreach ($filelist as $file)
13
2257
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 "mycode" instead of "python mycode.py". Great advice, worked like a charm. I recently jumped ship, and have been running Gentoo Linux for about...
18
2235
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 email. Preferably some documentation directly on this would be good but any other docuemtnation on executing a command line "command" would be...
3
4943
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 to use the 'Shell()'-command . This didn't work: Either I get an "File not found" error, or I get no error at all, but the PERL script isn't...
0
1453
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 Windows XP box, it does everything except logging and shell commands. If I execute the application after Windows XP boots up, then it runs just...
2
1821
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 exec("myprogram.exe $arg1 $arg2"); but then I realized my server is linux, and my program was compiled for
2
10275
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 @ Db_name.Packagename.function_name('p1','P2'); and after executing the shell scripts i am getting the follwing error:-ERROR at line 13: ORA-06550: line...
1
2356
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 directories in those parts must be done with group "design". This is currently done manually with "newgrp design" on the command line before doing...
2
4668
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 programe on 2nd AIX machine i had gone on some similar thread in this forum.... which includes ssh http://bytes.com/forum/thread579549.html
0
7580
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7634
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7945
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5481
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5208
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2079
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.