Solved

php script to save output as individual variables

Posted on 2014-04-01
7
509 Views
Last Modified: 2014-04-01
Hi Experts,

I have a php script in linux that is outputting the top 6 processes using up memory.

The script looks like this:

<?php

$top6 = shell_exec("top -b -n1 | head -13 | tail -n +8 | awk '{print$10}'");
echo $top6

?>

Open in new window


The output looks something like this:
0.1
0.3
0.0
0.0
0.0
0.0

I am very new to php and scripting and I would like to know (and get an example if possible), of how to save each of these values as an individual variable, without having to execute a shell command for each value.

Thanks in advance!
0
Comment
Question by:madstylex
  • 3
  • 2
  • 2
7 Comments
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 250 total points
ID: 39969061
Use
$arr = explode("\n", $top6);

Open in new window

This will create an array ($arr) by splitting your $top6 string at the new line character.

HTH,
Dan
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39969096
New to PHP?  Here is something to help you get started on a safe footing.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

The newline character is different in different operating systems.  It can be \n or \r or combinations of those.  If you want a context-aware newline character you can use PHP_EOL which is a predefined constant for the end-of-line character.  Professional programmers would choose that instead of "\n" out of habit, simply because it provides portability in your PHP scripts.

The $top6 variable is a string variable.  After explode() the $arr variable is an array variable.  If you want to save the values from the command, you can just save the string variable, perhaps by writing it to a file with file_put_contents().
0
 

Author Comment

by:madstylex
ID: 39969109
Thanks,

I have now done this to my script.

<?php


$top6name = shell_exec("top -b -n1 | head -13 | tail -n +8 | awk '{print$2}'}");
$top6value = shell_exec("top -b -n1 | head -13 | tail -n +8 | awk '{print$10}'");

$arrname = explode("\n", $top6name);
$arrvalue = explode("\n", $top6value);

?>

Open in new window


Would it then be possible to create an associative array by doing:

$graphvalues=array(

    $arrname => $arrvalue

);

Open in new window


I am trying to define the values in order to generate a graph.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 39969171
Yes, that makes sense.  Please see array_combine().  After you've combined the two arrays into an associative array, if you want to save the information, consider using json_encode() to generate a string that is universally recognizable.
0
 

Author Comment

by:madstylex
ID: 39969237
This is what I ended up with:

        #  Gathering the process name and % of memory usage

        $top6name = shell_exec("top -b -n1 | head -13 | tail -n +8 | awk '{print$12}'");
        $top6value = shell_exec("top -b -n1 | head -13 | tail -n +8 | awk '{print$10}'");

        # Creating 2 arrays with the 6 values in each

        $arrname = explode("\n", $top6name);
        $arrvalue = explode("\n", $top6value);

        #Combing arrays in order to use them in the graph generation

        $graphvalues = array_combine($arrname, $arrvalue);

Open in new window


$graphvalues was then used at another part of the script which generated the graph successfully.

I shall split the points between yourself and Dan as you both helped with different parts of the script.

Thanks guys!

PS:  Ray, thanks for the link to the PHP resource
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39969268
Glad I could help!

And Ray, thanks for the lesson :)
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39969292
Thanks for the points and thanks for using EE! ~Ray
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question