5

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>
hjpotter92
  • 78,589
  • 36
  • 144
  • 183

2 Answers2

1

Try running your command copying the output to the stdout:

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

shell_exec returns null if an error occurs running the command, using 2>&1 your are getting the whole output even if the command fails.

See sh command: exec 2>&1

Community
  • 1
  • 1
Saul Martínez
  • 920
  • 13
  • 28
  • I edited your post to format the code properly, but you should consider explaining what this does exactly. I'm sure an explanation would help the original poster as well as all the future visitors. Good luck! :) – jamesmortensen Oct 16 '12 at 08:16
  • Thanks for the advice! And I should add that I used that code when trying to get the output of `convert` for resizing images. And @ user1749264! you should try echoing the output ;) – Saul Martínez Oct 16 '12 at 08:22
  • Cool, It's awesome running bash from PHP ;) I know you've been a member of SO for awhile, but as a refresher, you can use this [edit] link to add the explanation to your answer. Hope this helps! :) – jamesmortensen Oct 16 '12 at 08:25
1

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")); ?>';
jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
core1024
  • 1,882
  • 15
  • 22