让PHP与每一行更新从shell命令返回 [英] Get PHP to update with every line returned from a shell command

查看:165
本文介绍了让PHP与每一行更新从shell命令返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:
我想写使用PHP将自动结帐几个大SVN回购的shell脚本。我也使用PEAR控制台进度条类来显示结帐的进展(不是完全必要的,但促使我的问题的东西)。

问:
有没有一种方法来运行一个循环,将每行输出到标准输入的命令行?

更新

如果我

 < PHP了shell_exec?(SVN合作的svn://my.repo.com/repo/trunk); ?>

我得到一个巨大的字符串后面都来自命令的输出。
尝试循环像

 < PHP的$栏=新Console_ProgressBar(%率(%)[%栏中%]%百分比%|%经过%::%估计%','​​=>'? ,'',80,$总);
    $酒吧,>显示器(0);
    stream_set_blocking(STDIN,0);
    $输出=阵列();
    $收益率='';
    EXEC(SVN共同$ svnUrl $文件夹,$输出,$回报);
    而(!使用isset($返程))
    {
        $酒吧,>更新(计数($输出));
    }
    $酒吧,>删除(); ?>

将显示栏,但不会更新。

任何解决方案?

========================= UPDATE ================== =====================

这里的工作code,我想出了基于答案(备查):

 < PHP
    $酒吧=新Console_ProgressBar(%率(%)[%栏中%]%百分比%|%经过%::%估计%','​​= GT;','',80,$总数);
    $处理=的popen(的/ usr / bin中/ SVN共同$ svnUrl $文件夹,R);
    $经过= 0;
    $酒吧,>显示($经过);    而($行=与fgets($处理))
    {
        $已过++;
        $酒吧,>更新($已过);
    }
    pclose函数($处理);
    ?>


解决方案

PHP是不是多线程。直到exec'd任务完成的exec()和公司会阻止你的脚本。有一个在使用whil​​e()循环没有意义的,因为循环甚至不会开始运行,直到SVN调用完成。

如果您要运行多个并行SVN的,你需要使用的popen()(或<一个HREF =htt​​p://php.net/proc_open相对=nofollow> proc_open()),它给你一个文件句柄,从中可以读取外部命令的输出,并具有在多个这样的外部命令同时

Background: I'm trying to write a shell script using php that will automatically checkout a couple of large SVN repos. I am also using the PEAR console progress bar class to display the progress of the checkout (not totally necessary, but the thing that prompted my question).

Question: Is there a way to run a loop that will update with every line output to STDIN on the commandline?

If I do

<?php shell_exec("svn co svn://my.repo.com/repo/trunk"); ?>

I get back all of the output from the command in a giant string. Trying a loop like

   <?php     $bar = new Console_ProgressBar('%fraction% [%bar%] %percent% | %elapsed% :: %estimate%', '=>', ' ', 80, $total);
    $bar->display(0);
    stream_set_blocking(STDIN, 0);
    $output = array();
    $return = '';
    exec("svn co $svnUrl $folder", $output, $return);
    while (!isset($return))
    {
        $bar->update(count($output));
    }
    $bar->erase(); ?>

will display the bar but not update.

Any solutions?

========================= UPDATE =======================================

Here's the working code I came up with based on the answer (for future reference):

    <?php    
    $bar = new Console_ProgressBar('%fraction% [%bar%] %percent% | %elapsed% :: %estimate%', '=>', ' ', 80, $total);
    $handle = popen("/usr/bin/svn co $svnUrl $folder", 'r');
    $elapsed = 0;
    $bar->display($elapsed);

    while ($line = fgets($handle))
    {
        $elapsed++;
        $bar->update($elapsed);
    }
    pclose($handle);
    ?>

解决方案

PHP is not multi-threaded. exec() and company will block your script until the exec'd task completes. There's no point in using the while() loop, because the loop will not even start running until the svn call has completed.

If you want to run multiple parallel svn's, you'll need to use popen() (or proc_open()), which gives you a filehandle from which you can read the external command's output, and have multiple such external commands at the same time.

这篇关于让PHP与每一行更新从shell命令返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆