Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing a bash script using php exec and javascript

I am trying to run this javascript below but am not seeing the output of the php shell_exec command.

Running the test -a bash script will output a series of ID's 34535, 25643, 23262, and so on. When I run it in my php file with a simple it works fine.

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

But when I try to run it below and I select test1 there is nothing outputted to the screen. Looking through chromes developer tools I see the code when test1 is selected as the following

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

as if it is commented out.

So my question is, is it possible to run the bash script this way with php and JavaScript? Or is there another way to get that information displayed to the webpage without 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>
like image 235
user1749264 Avatar asked Nov 03 '22 14:11

user1749264


1 Answers

If your result is multi line, you need to replace the newline marker with escape sequence or HTML. Try to replace

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

with

var tester = '<?php echo str_replace(PHP_EOL, '<br/>', shell_exec("/opt/bin/test -a")); ?>';
like image 152
core1024 Avatar answered Nov 13 '22 18:11

core1024