簡體   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