0

I was following the guide written here Run Bash Command from PHP

and I have /var/www/test.php

<?php $old_path = getcwd();
chdir('/home/');
$output = shell_exec('./test.sh');
echo "<pre>$output</pre>";
chdir($old_path); ?>

and located by using sudo nano /home/test.sh

#/bin/bash
mystring="Hello World"
echo "$mystring"

yet running locally http://localhost/test.php I get a blank page. I can run the file from the terminal, and I can run shell_exec('ls') successfully as php script, can anyone see what I am doing wrong please?

Community
  • 1
  • 1
Fuzzybear
  • 1,388
  • 2
  • 25
  • 42
  • 2
    you are not echoing your $output `echo $output;` – cmorrissey Jul 26 '13 at 16:08
  • right, following up on @ChristopherMorrissey either directly call shell_exec or output the `$output` – ಠ_ಠ Jul 26 '13 at 16:11
  • ok sorry that was a typo... updated the OP and I still have a blank page :( – Fuzzybear Jul 26 '13 at 17:16
  • 1
    And what happens if you just try the shell_exec command directly? – ಠ_ಠ Jul 26 '13 at 17:19
  • ahh permission denied... i need the chmod 700 ./test.sh i believe – Fuzzybear Jul 26 '13 at 19:18
  • hmm ran chmod +x /home/test.sh and that now produces Hello World in the terminal.. but web page still blank... – Fuzzybear Jul 26 '13 at 19:28
  • 1
    echo shell_exec('pwd')....maybe its the file location problem – hendr1x Jul 26 '13 at 19:31
  • I have identical code up on my server and it prints out "Hello" not "Hello World" so that a separate issue i'll look into but strange my local computer does not like it... is there some chmod I may have done on my local pc to cause it to behave this way?? – Fuzzybear Jul 26 '13 at 19:34
  • ahh yes that gave me a blank after I am supposed to have gone to /home tried ./home and that not working... is this a set chmod -x /home command time? – Fuzzybear Jul 26 '13 at 19:58

1 Answers1

0

Ok problem was resolved after a few typos and permissions not being set, but main problem it would seem is the getcwd() does not run on my OS locally or on the server... both Ubuntu 12.04

So for anyone else stuck with problems be sure to:

  • Check the File can be executed via the terminal, if not run chmod +x /file/location/test.sh
  • next check what directory you are in and moving to using the echo shell_exec('pwd')
  • If you are not moving directory then try replacing the getcwd() with dirname(__FILE__);

This resolved the issues for me :) now to ty passing variables into that script from the php page!! Many thanks for all your help everyone.

`

Fuzzybear
  • 1,388
  • 2
  • 25
  • 42