我正在从PHP执行shell脚本。我从php中的shell_exec方法向它发送参数。脚本确实接收参数并执行但是,if else条件将失败。我将R传递为$ 2,这也在脚本中正确回显但是if语句只是不执行该条件并运行else部分?当清楚地在回声中我可以看到R被打印一个s $ 2
当我从命令行传递相同的参数时,它可以工作。
executeshellscript.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
OutPut:
Input parameters: y1 R xyz abcd
Webreset:Do nothing:Invalid option
答案 0 :(得分:0)
应该是
elif [ "$2" == 'R' ]; then
见
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
了解更多信息
"
表示扩展内部即运行命令R
'
是字符串