system() function in PHP prints variable 2 times - php

Stupid question, this code:
<?php
$variable = system("cat /home/maxor/test.txt");
echo $variable;
?>
with file test.txt:
blah
prints:
blah
blah
What can I do with system() function to not print nothing so I get 1 "blah"???

system displays whatever the program outputs and returns the last line of output.
exec displays nothing and returns the last line of output.
passthru displays whatever the program outputs and returns nothing.

Use exec instead of system
http://us.php.net/manual/en/function.system.php#94262

According to the manual -- see system() :
system() is just like the C version
of the function in that it executes
the given command and outputs the
result.
Which explains the first blah
And :
Returns the last line of the command
output on success
And you are echoing the returned value -- which explains the second blah.
If you want to execute a command, and get the full output to a variable, you should take a look at exec, or shell_exec.
The first one will get you all the lines of the output to an array (see the second paramater) ; and the second one will get you the full output as a string.

Use exec instead. To get all the output, rather than just the last line do this:
$variable = array();
$lastline = exec("cat /home/maxor/test.txt", $variable);
echo implode("\n", $variable);

system is calling the actual cat program, which is outputting "blah" from test.txt. It also returns the value back to $variable which you're then printing out again.
Use exec or shell_exec instead of system.

Related

PHP exec() Output Cut Short

I have a PHP file that runs a node script using exec() to gather the output, like so:
$test = exec("/usr/local/bin/node /home/user/www/bin/start.js --url=https://www.example.com/");
echo $test;
It outputs a JSON string of data tied to the website in the --url paramater. It works great, but sometimes the output string is cut short.
When I run the command in the exec() script directly, I get the full output, as expected.
Why would this be? I've also tried running shell_exec() instead, but the same things happens with the output being cut short.
Is there a setting in php.ini or somewhere else to increase the size of output strings?
It appears the only way to get this working is by passing exec() to a temp file, like this:
exec("/usr/local/bin/node /home/user/www/bin/start.js --url=https://www.example.com/ > /home/user/www/uploads/json.txt");
$json = file_get_contents('/home/user/www/uploads/json.txt');
echo $json;
I would prefer to have the direct output and tried increasing output_buffering in php.ini with no change (output still gets cut off).
Definitely open to other ideas to avoid the temp file, but could also live with this and just unlink() the file on each run.
exec() only returns the last line of the output of the command you pass to it. Per the section marked Return Value of the following documentation:
The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.
To get the output of the executed command, be sure to set and use the output parameter.
https://www.php.net/manual/en/function.exec.php
To do what you are trying to do, you need to pass the function an array to store the output, like so:
exec("/usr/local/bin/node /home/user/www/bin/start.js --url=https://www.example.com/", $output);
echo implode("\n", $output);

PHP exec() execute last command only?

Here is an example trying to understand exec() function
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo exec("id;ls");
?>
When i run this code the result of ls only
Does exec() execute the last command only or it executes both of them and echo the last command ?
You can use shell_exec() instead for this purpose.
On the other hand, exec() returns only last line of the output (by default), but you can provide reference for output array as a second argument.
See the documentation for more info.
exec returns the last line from the result of the command. You have to use output argument. If the output argument is present, then the specified array will be filled with every line of output from the command.
exec("id;ls", $output);
var_dump($output);
you need write a shell script for Linux(excutable with .sh file)

exec("ls") not returning full "ls"

So I have this PHP code which is pretty simple.
$string = exec("ls foo");
In foo I have 4 files
foo
bar
hi
bye
But echo $string returns bye
How can I make it return all of the files? Is it not working because ls separates by tabs?
From the manual: http://php.net/manual/en/function.exec.php
Return Values
The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the ˋpassthru()ˋ function.
Please do not use exec for file operations. PHP has a full set of functions for that purpose. You can start with dir:
http://php.net/manual/en/function.dir.php
Boris' suggestion is good, but it's a bit complicated to figure it out.
Simply use this
passthru ('ls');
or this to 'prettify' it
echo "<pre>";
passthru ('ls');
echo "</pre>";

exec() not displaying correct output

I am running this test code using exec() in PHP
exec('#!/bin/bash');
exec('abc=10');
echo exec('echo $abc'); // no output
echo exec('whoami'); // this works fine
If I run the first 3 lines in terminal, the output is '10'.
But PHP does not output anything. What am I doing wrong?
http://php.net/manual/en/function.exec.php
Manual says use output_arr to capture output.
<?php
Exec ($cmd, $output_arr);
Print_r ( $output_arr);
?>
#zarathuztra is right. Try combining the various commands in a single exec with ; in between. Also check my answer if output_arr gets what you want.

How can we execute linux commands from php programs?

I want to see the output of the following code in the web browser:
code:
<?php
$var = system('fdisk -l');
echo "$var";
?>
When I open this from a web browser there is no output in the web browser. So how can I do this? Please help!
Thanks
Puspa
you can use passthru, like so:
$somevar = passthru('echo "Testing1"');
// $somevar now == "Testing1"
or
echo passthru('echo "Testing2"');
// outputs "Testing2"
use exec('command', $output);
print_r($output);
First of all, be sure that you (=user under which php runs) are allowed to call the external program (OS access rights, safe_mode setting in php.ini). Then you have quite a few options in PHP to call programs via command line. The most common I use are:
system
This function returns false if the command failed or the last line of the returned output.
$lastLine = system('...');
shell_exec or backtick operators
This function/operator return the whole output of the command as a string.
$output = shell_exec('...'); // or:
$output = `...`;
exec
This function returns the last line of the output of the command. But you can give it a second argument that then contains all lines from the command output.
$lastLine = exec('...'); // or capturing all lines from output:
$lastLine = exec('...', $allLines);
Here is the overview of all functions for these usecases: http://de.php.net/manual/en/ref.exec.php

Categories