When running shell scripts using php exec(), one script works (only git status) and the other is not available (executes git checkout). How did it come about?
I’m trying to set up a web-based portal through which we can check different branches of the Git repository by simply clicking on the backend panel.
Currently, I have /var/www/devportal, which contains index.php, status.sh, and checkout.sh
In index.php, I do the following:
$repo = $_GET['repo']; $command = 'sh status.sh ' . $repo; $output = exec($command); echo "<pre>$output</pre>";
The content of the status.sh is:
#!/bin/bash -e if [ $# -ne 1 ] then echo "Usage: `basename $0` <repo name>" exit 1 fi cd /var/www/$1 git status
This works great. The output in PHP shows the status of the current branch in /var/www/proj.
Now, when I try to do the same thing with checkout.sh (this time passing 2 arguments, the second argument is the name of the branch to checkout), it says :
#!/bin/bash -e if [ $# -ne 2 ] then echo "Usage: `basename $0` <repo name> <branch name>" exit 1 fi cd /var/www/$1 git checkout $2
This will not work. Not only does it not work, but I don’t crouch with error messages. There is no output. I know the checkout.sh script works fine because when I echo the command sent via PHP’s exec command, copy that exact thing and run it through a terminal logged in as root, it works just fine, doing checkout and returning the name of the newly activated branch.
Any tips would be appreciated. My box is pretty standard, Ubuntu 10.04 and running Apache2.
exec populates the $output with the standard output of your command to show errors, if any, by adding “2>&1” to the end of the command.
exec can also tell you the return value, try:
$output = exec($command, $array_output, $ret_val); var_dump($ret_val); echo "<pre>$output</pre>";