Connecting Tech Pros Worldwide Forums | Help | Site Map

Command Line Progress Percent Display, no echo

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#1: Jun 23 '08
I want to display the progress of a job (a cron job) in PHP.

I can do an echo, but the result will look like this:

1%2%3%4%

How do you "backspace" and over-write the percentage with the new value.

I don't know where to begin looking for this. What do you even call this to do a Google search?

Dan

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#2: Jun 23 '08

re: Command Line Progress Percent Display, no echo


I found this:
http://snippets.dzone.com/posts/show/3760

How can i get that to work in PHP. shell_exec() ?
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#3: Jun 23 '08

re: Command Line Progress Percent Display, no echo


Just add a \r in front of you output. That should cause the echo to overwrite the previous line.
Like:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. for($i = 0; $i < 100; $i++) {
  3.     echo "\r$i% Completed...";
  4.     usleep(200000);
  5. }
  6. ?>
  7.  
That is assuming you are displaying this in a Linux terminal?
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#4: Jun 24 '08

re: Command Line Progress Percent Display, no echo


Quote:

Originally Posted by Atli

Just add a \r in front of you output. That should cause the echo to overwrite the previous line.
Like:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. for($i = 0; $i < 100; $i++) {
  3.     echo "\r$i% Completed...";
  4.     usleep(200000);
  5. }
  6. ?>
  7.  
That is assuming you are displaying this in a Linux terminal?

Thanks, that was easy.

yes of course Linux :D
Reply