简体   繁体   中英

How to run shell script in php

I want to run a shell script from run_bash_script.php

Code of run_bash_script.php:

<?php

$old_path = getcwd();
chdir('C:\xampp\htdocs\RTLS_Final\2ndRTLS');
$output = shell_exec('./bashscript.sh');
chdir($old_path);
echo "$old_path\n";
echo "<pre>$output</pre>\n"; 
?>

Ouput: C:\\xampp\\htdocs\\RTLS_Final\\2ndRTLS

code of bashscript.sh

STRING="Solar Eclipse"
# print the contents of the variable on screen
echo $STRING

python pythoncode.py

code of pythoncode.py

from scipy.optimize import minimize
import numpy as np

def gps_solve(distances_to_station, stations_coordinates):
    def error(x, c, r):
        return sum([(np.linalg.norm(x - c[i]) - r[i]) ** 2 for i in range(len(c))])
    l = len(stations_coordinates)
    S = sum(distances_to_station)
    # compute weight vector for initial guess
    W = [((l - 1) * S) / (S - w) for w in distances_to_station]
    # get initial guess of point location
    x0 = sum([W[i] * stations_coordinates[i] for i in range(l)])
    # optimize distance from signal origin to border of spheres
    return minimize(error, x0, args=(stations_coordinates, distances_to_station), method='Nelder-Mead').x

def calculateDistance(rssi):
  txPower = -59 #hard coded power value. Usually ranges between -59 to -65
  if rssi == 0:
    return -1.0

  ratio = rssi*1.0/txPower
  if (ratio < 1.0):
    return ratio**10
  else:
    distance =  (0.89976)*ratio**7.7095 + 0.111
    return distance

# print(calculateDistance(-71))
if __name__ == "__main__":
    # stations = list(np.array([[1,1], [0,1], [1,0], [0,0]]))
    stations = list(np.array([[13.39026121057006,52.51264336892332], [13.39033590842385,52.512538230285614], [13.390433408780382,52.51264251710566]]))
    distances_to_station = [calculateDistance(-71), calculateDistance(-69), calculateDistance(-45)]
    # return gps_solve(distances_to_station, stations)
    print(gps_solve(distances_to_station, stations))

When I run bash script from git the output is:

从 git 运行 bash 脚本的输出

You should put in shell_exec() literally what you type in your shell.

That being said. Put bash before the script path.

// ... original code ....

$output = shell_exec('bash ./bashscript.sh');

// ... original code continues ....

要运行 php 脚本,您应该像这样运行

php run_bash_script.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM