从PHP向shell脚本发送参数不起作用


Sending paramters to shell script from PHP not working?

我正在从PHP执行shell脚本。我从php中的shell_exec方法向它发送参数。脚本确实接收到参数并执行。但是,if-else条件将失败。我将R作为$2传递,这在脚本中也得到了正确的响应,但if语句不会执行该条件,也不会运行其他部分?当清晰地出现在回声中时,我可以看到R被打印成s$2的

当我从命令行传递相同的参数时,它就起作用了。

executesellscript.php

<?php
/** this code handles the post ajax request**/
if(isset($_POST['getAjax'])) {
    /* you can do this below settings via your php ini also. no relation with our stuff */
    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting(-1);
    /* setting content type as json */
    header('Content-Type: application/json');
    $blueid = $_POST['bluemixid'];
    $bluepwd = $_POST['bluemixpswd'];
    $env =  $_POST['env'];
    $restart =  $_POST['action'];
    $result = shell_exec('sh /var/www/shellscriptphp/webreset.sh    '.$env.'    '.$restart.'    '.$blueid.'  '.$bluepwd.'   ');
    /* making json string with the result from shell script */
    echo json_encode(array("result"=>$result));
    /* and we are done and exit */
    exit();
}
?>

函数executeShellScript(单击){

var blueid = $('#bluemixid').val();
var bluepwd= $('#bluemixpassword').val();
var env = $('#envoption').val();
var action = $('#actionoption').val();
//$_SERVER["REQUEST_URI"] is used to refer to the current page as we have the ajax target as this same page
$.post('<?PHP echo $_SERVER["REQUEST_URI"]; ?>',{"getAjax":true,"blueid":blueid,"bluepswd":bluepwd,"env":env,"action":action }, function(data) {
   // alert(data['result']);
    $("#textarea_message").val(data['result']);
    return false;
});

}

html

<tr><td>
What action you wish to perform ?
</td><td>
<select id="actionoption" >
  <option value="none">      </option>
  <option value="R" >Restart Resource Manager </option>
  <option value="O"> Restart Olympia Server </option>
  <option value="B"> Restart  Both  </option>
</select>
</td></tr>
<br>

webreset.sh

#!/bin/bash
# Input parameters $2 Component
echo "Input parameters: $1 $2 $3 $4"
if [ "$2" == "O" ]; then
        setRM
        restartO
        setRO
elif [ "$2" == "R" ]; then
        setrMai
        resRM
        setOperation
elif [ "$2" == "B" ]; then
        setMaintenance
        rtOlympia
        resRM
        setOperation
else
        echo "Webreset:Do nothing:Invalid option"
fi

输出:

Input parameters: y1 R xyz abcd
Webreset:Do nothing:Invalid option

应该是

elif [ "$2" == 'R' ]; then

参见

http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

更多信息

"表示展开内部,即运行命令R

'是字符串