dtbiszu7724 2012-06-25 10:11
浏览 192
已采纳

当使用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!

  • 写回答

2条回答 默认 最新

  • dongtan7351 2012-06-25 10:26
    关注

    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>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?