简体   繁体   English

从PHP执行bash脚本

[英]Executing bash script from php

I am trying to execute a bash script from within a php script. 我正在尝试从php脚本中执行bash脚本。

The php script is: php脚本是:

    <?php

    $clientName = $_POST['clientName'];
    $startDate = $_POST['startDate'];
    $endDate = $_POST['endDate'];
    $mode = $_POST['mode'];

    echo("Current working directory = " . getcwd());
    echo("Client Name = " . $clientName . "<br/>\n");
    echo("Start date = " . $startDate . "<br/>\n");
    echo("End date = " . $endDate . "<br/>\n");
    echo("Mode = " . $mode . "<br/>\n");

    $cmd = "/webroot/argRepeater.bash escapeshellarg($clientName) escapeshellarg($startDate) escapeshellarg($endDate) escapeshellarg($mode)";

echo("Command = " . $cmd . "<br/>\n");
var_dump($cmd);

    exec("/bin/bash ./argRepeater.bash escapeshellarg($clientName) escapeshellarg($startDate) escapeshellarg($endDate) escapeshellarg($mode)", $output, $output2);

    echo("Output array = " . print_r($output) . "<br/>\n");
    echo("Output = " . $output2 . "<br/>\n");

    ?>

The above php script takes arguments from an html form. 上面的php脚本从html表单获取参数。 The bash script argRepeater.bash only repeats whatever arguments were given to it. bash脚本argRepeater.bash仅重复提供给它的任何参数。 The output is as follows: 输出如下:

 Current working directory = /home/content/31/10199331/htmlClient Name = yum
    Start date = 2013-05-14
    End date = 2013-05-24
    Mode = fir
   Command = ./argRepeater.bash escapeshellarg(yum) escapeshellarg(2013-05-14) escapeshellarg(2013-05-24) escapeshellarg(fir)
string(128) "./argRepeater.bash escapeshellarg(yum) escapeshellarg(2013-05-14) escapeshellarg(2013-05-24) escapeshellarg(fir)" Array ( ) Output array = 1
    Output = 1  

My questions: 我的问题:
1. What more needs to be done to ensure argRepeater gets executed? 1.还需要做些什么来确保argRepeater被执行?
2. How do I display argRepeater's outputs on the webpage? 2.如何在网页上显示argRepeater的输出?

1. What more needs to be done to ensure argRepeater gets executed? 1.还需要做些什么来确保argRepeater被执行?

Well it needs all that it needs. 好吧,它需要它所需要的。 Troubleshoot your needs if you are not so sure if it worked out or not. 如果您不确定是否可行,请对您的需求进行故障排除。 Output = 1 signals it did work in the sense that something was executed. Output = 1表示它确实执行了某种操作

2. How do I display argRepeater's outputs on the webpage? 2.如何在网页上显示argRepeater的输出?

You already do it: 您已经做到了:

echo("Output array = " . print_r($output) . "<br/>\n");

so as well, you ask about something that already is solved by your own code. 因此,您也会询问自己的代码已解决的问题。

So probably the problem is just that you are uncertain if you understand your code or not? 因此,可能的问题就是您不确定是否了解自己的代码? If so, just double check every statement you write with the manual. 如果是这样,只需仔细检查您用手册编写的每条语句。

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

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