2

I'm performing an egrep in Linux using PHPs exec(). The output is a list of files when actually executing this statement from the command line in Linux. When I echo the result in PHP it only displays the last line of the output.

I want to bind this list to an array so I can further manipulate it in PHP. I've tried using array() to bind the output to an array and then display it with print_r, but it only shows one item in the array, the last line.

hakre
  • 193,403
  • 52
  • 435
  • 836
blarg
  • 3,773
  • 11
  • 42
  • 71
  • 1
    There is no such thing as array binding in PHP. However everything you want to learn about with exec should be outlined at http://php.net/function.exec - Apart from that, the code you describe the problem about is missing. Also duplicate material that shows how this is done does likely exists already on this website, please use the search before asking such a general question. – hakre Sep 09 '13 at 09:45
  • In case you need a screenshot to understand the manual, here is a related example: http://stackoverflow.com/a/8662054/367456 – hakre Sep 09 '13 at 09:52
  • 1
    possible duplicate of [exec function not returning all](http://stackoverflow.com/questions/6339269/exec-function-not-returning-all) – Elias Van Ootegem Sep 09 '13 at 09:53

1 Answers1

2

Read the manual, and look at the signature:

string exec ( string $command [, array &$output [, int &$return_var ]] )

Thus:

$lastLine = exec('ls', $output, $status);
var_dump($output);

Is what you're after!

exec indeed returns the last line of output, but it accepts 3 arguments. The second is a reference to an array (for some reason, it needn't be declared beforehand, no notices are issued here). The third is the exit status (sort of like you'd get when you do anecho $? after a command/program has finished running on your command-line.
To quote the manual:

If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().

This means, the best use (as in get the most info) of exec is this:

$last = exec('ls -lta', $fullList, $status);
if ($status !== 0)
{
    exit('command didn\'t finish as expected: '.$status);
}
print_r($fullList);

If this still isn't making sense to you, then refer to any of the duplicate questions on this site

Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • Just FYI: *for some reason,*: The reason is that you declare it if you write it there as it passed by reference, all you need to do is to write the variable name there. Done. – hakre Sep 09 '13 at 09:55
  • @hakre: Ok, I sort of expected a notice when passing a var name to a function with signature `array &$var`. Declaring a variable initializes it to `null`, I thought... – Elias Van Ootegem Sep 09 '13 at 09:56