Good Day everyone
Having a bit of a "Head Bang"on this. Im trying to find a way to search through files for a certain string. I know how to do it in linux by using a simple grep -c, but i'm unable to assign varaibles to the output. For example I want the file name as $var1 and the count as $var2, the reason for this is I will be using the data in a chart. I'm not to fixed on using linux commands, if there is any better way to do so I am open to using it. I would appreciate any info or direction of where to go and read up. My code:
<?php
$output = shell_exec ( "grep -c 'detected Tag 10055' *.txt" )
?>
This would the give me the file path and count as ../../public/130512.txt:100.
Thanks for any info
Use the another method in PHP to capture the output of the external program
string exec ( string $command [, array &$output [, int &$return_var ]] )
For example
$command = 'ls -ltr';
$response = exec ( $command, $output , $return );
print_r($output);
print_r($return);
The variable output array will contain your execution output, from that you can take the result.
The $response contains your last line of external program's output
$output contains the array of all output line by line
$return is the return value of your external program
Related
I am having difficulty processing bash shell in php.
My problem is as below.
I have 3 tasks corresponding to 3 files bash shell (task1.sh, task2.sh, task3.sh). When task1.sh finishes processing, task2.sh will automatically execute, when task2.sh finishes processing, task3.sh will automatically execute.
Initially, I wrote a file called task.sh and embedded task1.sh, task2.sh, task3.sh into it. But I want to embed these 3 tasks into a php file.
For example: I create task.php and do the following:
If task1.sh fails, it will display popout (alert) error message.
If task1.sh processing is complete then task2.sh will continue to be automatically done.
The processing of task2.sh and task3.sh is similar to the above.
All 3 tasks I want to run backgroud. The problem is that when I run the background bash shell, I will not be able to check the failed error statement (the result always returns to 0).
I have learned a lot and consulted many documents but it did not help me.
I hope you can support me.
Sorry, my english very poor.
You may use the $retval argument of exec().
<?php
exec('task1.sh', $output, $retval);
if ($retval !== 0) {
// task 1 failed
exit('Error running task1: ' . implode("<br/>\n", $output));
}
exec('task2.sh', $output, $retval);
if ($retval !== 0) {
// task 2 failed
exit('Error running task1: ' . implode("<br/>\n", $output));
}
exec('task3.sh', $output, $retval);
if ($retval !== 0) {
// task 3 failed
exit('Error running task1: ' . implode("<br/>\n", $output));
}
You can simply use the function:
exec ( string $command [, array &$output [, int &$return_var ]] ) : string
With:
output :
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().
return_var :
If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.
Thus you could use $output and $result_var to check the execution errors of your shells.
Regards
Just wondering if there is a setting that I am missing in my curl command that is preventing the data from coming back as a json string and instead returning it as an array.
$cmd='curl -d #/home/wazilly/public_html/recipe.json -H "Content-Type:
application/json" "https://api.edamam.com/api/nutrition-details?
app_id=XXXXX&app_key=97d3b48d3a8366572c8012a142e28f50"';
exec($cmd,$result);
echo $result; //prints Array
According to the documentation, exec always takes an array as its second parameter, which it fills with all the lines of output of the executed command.
I'm not certain what the output of your command would be like, though. You'd be better off doing a var_dump() on the $result to be certain. But since you're dealing with curl, why not simply use the PHP curl extension ?
Check the prototype for exec() in the PHP manual:
string exec ( string $command [, array &$output [, int &$return_var ]] )
PHP returns an array. If you want the raw output instead, which I assume might actually be JSON, try this instead:
$result = shell_exec($cmd);
echo $result;
I suggest that you convert $result array to JSON format using json_encode(array())
echo json_encode($result);
I am unable to get php to show full results of my python script, only the last basic print statement is printed. What am I doing wrong here? Why is the php not showing the output from the python script?
php
$result = exec('python list.py');
echo $result;
list.py
import subprocess
print "start"
a = subprocess.Popen(["ls", "-a"], stdout=subprocess.PIPE)
a.wait()
result_str = a.stdout.read()
print result_str
print "stop"
the command line output for that python script is as below
start
...filename
...filename
...etc...etc
stop
The php output from executing the python script is only
stop
Thank you
You need to pass $result as a param into exec() otherwise you only get the last statement.
exec('python list.py', $result);
print_r($result);
This will get an array of output, $result[0] will contain stop and $result[sizeof($result)-1] will contain stop and everything in between will contain the rest of your program output.
exec will always return the last line from the result of the command. So, if you need to get every line of output from the command executed. Then, you need to write as below :
$result = exec('python list.py', $ouput);
print_r($output)
As per exec documentation
string exec ( string $command [, array &$output [, int &$return_var ]] )
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().
I am using a ffmpeg command for watermarking a video.
it does the work but i need to detect wheather it is executed successfully or not.
my command:
$mark = "ffmpeg -i ".$inputvideo." -i logo.png -filter_complex ". '"overlay=x=(main_w-overlay_w):y=(main_h-overlay_h)"'." ".uniqid()."html56.mp4";
For output i used something like:
$x = exec($mark);
print_r($x);
But i am not getting anything printed in place of $x.
After some searching I found this statement for exec command
string exec ( string $command [, array &$output [, int &$return_var ]] )
$s=exec($mark,$var);
$var is my return var.
Now when i print $var i am getting an empty array.
Please suggest where i am missing.
string exec ( string $command [, array &$output [, int &$return_var ]] )
When you do $s = exec($mark, $var); your $var corresponds to $output which is empty because ffmpeg outputs information to sderr since stdout might be used for actual data output.
If you want to get the return code all preceding optional arguments must be specified even if you don't use them:
$s = exec($mark, $output, $var)
If you need the actual output you can redirect the stderr to stdout since you're not using it by placing an 2>&1 at the end of your command or by using PHP's proc_open() to execute it.
I have a simple php exec command that calls svnlook. If I run the command through the terminal I get all the output I expect. If I run it as shown below, I only get the last item.
$list = exec("svnlook changed -r ".$urlCleaned." ".$SVNEXPORT);
echo $list;
Can I buffer the output? If so how? And will that help?
That's by design and is explained:
string exec ( string $command [, array &$output [, int &$return_var ]] )
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.
To get the output of the executed command, be sure to set and use the output parameter.
http://php.net/manual/en/function.exec.php
exec("svnlook changed -r ".$urlCleaned." ".$SVNEXPORT, $output);
var_dump($output);
Alternatively, shell_exec returns everything.