?
Solved

php script to save output as individual variables

Posted on 2014-04-01
7
Medium Priority
?
526 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
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 
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

Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

Question has a verified solution.

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

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
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.
Suggested Courses

770 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