Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

php script to save output as individual variables

Posted on 2014-04-01
7
Medium Priority
?
536 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 1000 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 111

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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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 111

Expert Comment

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

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

609 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