-   Programming (
-   -   Progress Bar (

zael 09-30-2003 01:45 PM

Progress Bar
Does anyone know how i can give the effect of a progress bar in a Linux Script while running a process?


crabboy 09-30-2003 04:17 PM

Here are two hacks at what you are looking for:

The first will call the process (in my test case a sleep) and print # marks every second until the process ends. This bar really only gives the user a hint that something is still working. He/she has no clue how long it will take.


sleep 8&  # put the calling processes here

while [ 1 ]; do
  sleep 1
  ps -ef | grep $PROCPID | grep -vq grep
  if [ $? -eq 1 ]; then
      echo -n "#"

This second attempt will display hash marks from 0% to 100%. The trick here it to know about how long the process will take. If it consistently takes about 10 seconds then add it to the AVG_TIME var. If you have no clue how long it will take then you probably should use the script above.



STEP=`expr 50 / $AVG_TIME`
echo    "          |0%--------------------50%---------------------100%|"
echo -n "Progress:  "
while [ $COUNT -lt $AVG_TIME ]; do
  sleep 1
  while [ $HASH -lt $STEP ]; do
      echo -n "#" 
      HASH=`expr 1 + $HASH`
  COUNT=`expr 1 + $COUNT`

UltimaGuy 10-01-2003 06:12 AM

Well, that was a cool one crabboy. I am very much impressed by this one.

david_ross 10-01-2003 12:20 PM

Another way to do it is spawn a process that loops and updates the display based on the contents of a file and then dies when the file contains certain text eg "STOP". The actual program that is doing the work can then echo the makers eg #s to the file at strategic points in the process.

All times are GMT -5. The time now is 09:53 PM.