1

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");
Community
  • 1
  • 1
Daniel W.
  • 31,164
  • 13
  • 93
  • 151

2 Answers2

7

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";
?>
Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
  • Change `$i <= 100` or else the script will end in 95% i guess – Sal00m Nov 15 '13 at 09:18
  • doesn't really matter, it's just an example – Gerald Schneider Nov 15 '13 at 09:18
  • I was curious weather the PHP echo works because I cannot set the `-ne` from the system's echo. I should have tried.. =) – Daniel W. Nov 15 '13 at 09:19
  • 1
    You could have looked up what `-ne` does. The bash echo command always adds `\n` at the end. `-n` ommits the trailing newline. `-e` enables the interpretion of backslash escapes. Without it the `\r` would have no effect. The PHP echo needs none of these options. – Gerald Schneider Nov 15 '13 at 09:22
1

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.

asdfklgash
  • 35
  • 6