Solved

php script to save output as individual variables

Posted on 2014-04-01
7
483 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 34

Accepted Solution

by:
Dan Craciun earned 250 total points
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
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
Comment Utility
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 34

Expert Comment

by:Dan Craciun
Comment Utility
Glad I could help!

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

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points and thanks for using EE! ~Ray
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now