当使用php exec()运行shell脚本时,一个脚本工作(它只是git状态)而一个脚本没有(它执行git checkout)。 怎么会?

当使用php exec()运行shell脚本时,一个脚本工作(它只是git状态)而一个脚本没有(它执行git checkout)。 怎么会?

问题描述:

I am trying to setup a web-based portal through which we can checkout different branches of our Git repository through a simple click on a back-end panel.

So 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 contents of status.sh are:

#!/bin/bash -e
if [ $# -ne 1 ]
then
    echo "Usage: `basename $0` <repo name>"
    exit 1
fi
cd /var/www/$1
git status

And this works just fine. The output echoed in PHP shows me the status of the current branch within /var/www/proj.

Now when I try to do the same thing (passing 2 parms this time with the second one being the name of the branch to checkout) with checkout.sh, who'se contents are:

#!/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

It doesn't work. Not only does it not work, I don't get diddly squat for an error message. There is no output. I know that the checkout.sh script works fine because when I echo the command that is being sent via PHP's exec command, copy that exact thing and run it via terminal logged in as root, it works just fine, does the checkout and returns the name of the newly activated branch.

Any tips on this would be greatly appreciated. My box is pretty standard, Ubuntu 10.04 and running Apache2.

Thanks!

我正在尝试设置一个基于Web的门户,我们可以通过它来检查我们的Git存储库的不同分支 点击后端面板。 p>

目前,我的/ var / www / devportal包含 index.php strong>, status.sh 和 checkout.sh strong> p>

index.php strong>中,我执行以下操作: p> $ repo = $ _GET ['repo']; $ command ='sh status.sh'。 $ repo; $ output = exec($ command); echo“&lt; pre&gt; $ output&lt; / pre&gt;”; code> pre>

status.sh strong>是: p>

 #!/ bin / bash -e 
if [$#-ne 1] 
then 
 echo“用法:  `basename $ 0`&lt; repo name&gt;“
退出1 
fi 
cd / var / www / $ 1 
git status 
  code>  pre> 
 
 

这很好用。 在PHP中回显的输出向我显示当前分支在/var/www/proj.

nn中的状态。当我尝试做同样的事情时(这次用第二个传递2个parms) 一个是结账的名称,用 checkout.sh strong>,其中包含的内容是: p>
 #!/ bin / bash -e  
if [$#-ne 2] 
then 
 echo“用法:`basename $ 0`&lt; repo name&gt;&lt; branch name&gt;”
 exit 1 
fi 
cd / var / www / $ 1 
git checkout $ 2  
  code>  pre> 
 
 

它不起作用。 它不仅不起作用,我不会因为错误消息而蹲下。 没有输出。 我知道checkout.sh脚本工作正常,因为当我回显通过PHP的exec命令发送的命令时,复制那个确切的东西并通过以root身份登录的终端运行它,它工作正常,检查并返回 新激活的分支的名称。 p>

对此的任何提示将不胜感激。 我的盒子非常标准,Ubuntu 10.04并运行Apache2。 p>

谢谢! p> div>

exec fills $output with your command standart output, to show error (if any) add "2>&1" at the end of your 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>";

$repo = $_GET['repo'];
$command = 'sh status.sh ' . $repo;
$output = exec($command);

Oh jesus man. Don't do this. escapeshellarg exists for a reason