• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 544
  • Last Modified:

php script to save output as individual variables

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
madstylex
Asked:
madstylex
  • 3
  • 2
  • 2
2 Solutions
 
Dan CraciunIT ConsultantCommented:
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
 
Ray PaseurCommented:
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
 
madstylexAuthor Commented:
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
Ray PaseurCommented:
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
 
madstylexAuthor Commented:
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
 
Dan CraciunIT ConsultantCommented:
Glad I could help!

And Ray, thanks for the lesson :)
0
 
Ray PaseurCommented:
Thanks for the points and thanks for using EE! ~Ray
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now