简体   繁体   English

如何在php中运行shell脚本

[英]How to run shell script in php

I want to run a shell script from run_bash_script.php我想从run_bash_script.php运行一个 shell 脚本

Code of run_bash_script.php: 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输出: C:\\xampp\\htdocs\\RTLS_Final\\2ndRTLS

code of bashscript.sh bashscript.sh 的代码

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

python pythoncode.py

code of pythoncode.py 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 脚本时,输出是:

从 git 运行 bash 脚本的输出

You should put in shell_exec() literally what you type in your shell.您应该按照字面意思输入shell_exec()键入的内容。

That being said.话虽如此。 Put bash before the script path.bash放在脚本路径之前。

// ... original code ....

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

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

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

php run_bash_script.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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