Executing Shell

Discussion in 'Perl' started by speedster, Mar 15, 2005.

  1. speedster

    speedster Guest

    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
    speedster, Mar 15, 2005
    #1
    1. Advertising

  2. speedster

    Jim Gibson Guest

    In article <>,
    speedster <> 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 =---
    Jim Gibson, Mar 17, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?emlubw==?=

    Shell not executing

    =?Utf-8?B?emlubw==?=, Dec 30, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    416
    =?Utf-8?B?emlubw==?=
    Dec 30, 2004
  2. Kentor

    executing shell commands from c?

    Kentor, Mar 19, 2007, in forum: C Programming
    Replies:
    10
    Views:
    771
    Ben Pfaff
    Mar 20, 2007
  3. Replies:
    1
    Views:
    359
    Victor Bazarov
    Jul 23, 2007
  4. Siberia

    shell for executing java progs

    Siberia, Jul 31, 2007, in forum: Java
    Replies:
    2
    Views:
    293
    Roedy Green
    Jul 31, 2007
  5. Svenn Are Bjerkem
    Replies:
    4
    Views:
    828
    Karthik Gurusamy
    Jan 13, 2008
Loading...

Share This Page