繁体   English   中英

使用php exec和javascript执行bash脚本

[英]Executing a bash script using php exec and javascript

我试图在下面运行这个javascript但是没有看到php shell_exec命令的输出。

运行测试-a bash脚本将输出一系列ID为34535,25643,23262等。 当我在我的PHP文件中运行它与一个简单的工作正常。

print shell_exec('/opt/bin/echkchunk -a');

但是当我尝试在下面运行它并选择test1时,没有任何内容输出到屏幕上。 通过chromes开发人员工具,我可以看到选择test1时的代码如下

<!--?php shell_exec("/opt/bin/echkchunk -a"); ?-->

好像它被注释掉了。

所以我的问题是,是否可以使用php和JavaScript以这种方式运行bash脚本? 或者有没有其他方法可以在没有JavaScript的情况下将这些信息显示在网页上?

<script type="text/javascript">
  var tester = '<?php shell_exec("/opt/bin/test -a"); ?>';      
     $(document).ready(function() {
            $("#selector").on("change", function() {
                    if ($("#selector").val() == "test1") {
                        $("#rightselection").css("background-color", "red");
                        $("#rightselection").html(tester);
                    }
                    if ($("#selector").val() == "test2") {
                        $("#rightselection").css("background-color", "blue");
                        $("#rightselection").html("test2");
                    }
                    if ($("#selector").val() == "test3"){
                        $("#rightselection").css("background-color", "yellow");
                        $("#rightselection").html("test3");
                    }
                    if ($("#selector").val() == ""){
                        $("#rightselection").css("background-color", "green");
                        $("#rightselection").html("This is a Test");
                    }
            });
    });
</script>

尝试运行将输出复制到stdout的命令:

<?php shell_exec("/opt/bin/echkchunk -a 2>&1"); ?>

如果运行命令时发生错误,则shell_exec返回null,使用2>&1即使命令失败,您也将获得整个输出。

请参阅sh命令:exec 2>&1

如果结果是多行,则需要使用转义序列或HTML替换换行符。 尝试更换

var tester = '<?php shell_exec("/opt/bin/test -a"); ?>';

var tester = '<?php echo str_replace(PHP_EOL, '<br/>', shell_exec("/opt/bin/test -a")); ?>';

暂无
暂无

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

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