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");

2 Answers 2

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";
?>
Sign up to request clarification or add additional context in comments.

4 Comments

Change $i <= 100 or else the script will end in 95% i guess
doesn't really matter, it's just an example
I was curious weather the PHP echo works because I cannot set the -ne from the system's echo. I should have tried.. =)
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.