I have a PHP script where I want to show its progess. From this super question and perfect answer How to add a progress bar to a shell script? I tried to emulate the behaviour:

shell_exec("echo -ne '######      30%'\r");

But nothing gets printed to the screen.

My guess is this is because STDOUT not correct, or I have to echo the echo like?

echo shell_exec("echo -ne '######      30%'\r");
有帮助吗?

解决方案

To use this in a php shell script you don't need to execute any shell commands at all. Just echo the output ending with a \r

echo "######      30%\r";

example script:

<?php
for ($i = 0; $i < 100; $i += 5) {
  $bar = str_repeat("#", $i/10);
  echo "$i% $bar \r";
  sleep(1);
}
echo "\n";
?>

其他提示

There is a good example for a progress bar in PHP command line interface: http://brian.moonspot.net/php-progress-bar

It is directly done in PHP without system calls.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top