Note that there are some explanatory texts on larger screens.

plurals
  1. POGet PHP to update with every line returned from a shell command
    text
    copied!<p><strong>Background:</strong> 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).</p> <p><strong>Question:</strong> Is there a way to run a loop that will update with every line output to STDIN on the commandline?</p> <p>If I do</p> <pre><code>&lt;?php shell_exec("svn co svn://my.repo.com/repo/trunk"); ?&gt; </code></pre> <p>I get back all of the output from the command in a giant string. Trying a loop like </p> <pre><code> &lt;?php $bar = new Console_ProgressBar('%fraction% [%bar%] %percent% | %elapsed% :: %estimate%', '=&gt;', ' ', 80, $total); $bar-&gt;display(0); stream_set_blocking(STDIN, 0); $output = array(); $return = ''; exec("svn co $svnUrl $folder", $output, $return); while (!isset($return)) { $bar-&gt;update(count($output)); } $bar-&gt;erase(); ?&gt; </code></pre> <p>will display the bar but not update.</p> <p>Any solutions?</p> <p><strong>========================= UPDATE =======================================</strong></p> <p>Here's the working code I came up with based on the answer (for future reference):</p> <pre><code> &lt;?php $bar = new Console_ProgressBar('%fraction% [%bar%] %percent% | %elapsed% :: %estimate%', '=&gt;', ' ', 80, $total); $handle = popen("/usr/bin/svn co $svnUrl $folder", 'r'); $elapsed = 0; $bar-&gt;display($elapsed); while ($line = fgets($handle)) { $elapsed++; $bar-&gt;update($elapsed); } pclose($handle); ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload