473,909 Members | 4,332 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 5000
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
10927
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
2294
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 two months. Is it possible to get the same behavior on Linux? I thought it would have something to...
18
2255
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 great! Thanks,
3
4994
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 executed anyway. The strange thing is that I managed to execute a .bat file for example. Why the hell...
0
1473
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 fine. It seems as if the app is not getting its inherited functionality at boot time. Any help is...
2
1842
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
10302
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 13, column 2: PLS-00221: 'function_name' is not a procedure or is undefined
1
2384
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 anything else. I have been looking for a way to execute this command as a part of a script, but it...
2
4688
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
10035
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9877
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
11046
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10538
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8097
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7248
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6138
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3357
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.