LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   progress bar instead of % complete in my for loop (https://www.linuxquestions.org/questions/programming-9/progress-bar-instead-of-complete-in-my-for-loop-846538/)

genderbender 11-25-2010 11:16 AM

progress bar instead of % complete in my for loop
 
I've got a loop which is able to echo out how complete a process is after each loop (e.g says 25% if the loop has 4 elments and has run once through).

Is there any way I can print characters, perhaps a progress bar? I was thinking of using the percentage as a count or something and using.

Code:

I got told off for including code from work here :P

genderbender 11-25-2010 11:21 AM

Lol, ok this is a total mess - %'s are output as follows:

Code:

16% 20% 25% 33% 50% 100%
The jump from 50% to 100% suggests I've made a monumental cock up somewhere. Can anyone help?

GrapefruiTgirl 11-25-2010 11:48 AM

Problem here is contributing to the progression of the percentages:
Code:

((STATUS= 100 / COUNT_VMS ))
At very least, I would do this:
Code:

STATUS=$(( 100 / COUNT_VMS ))
However, the full equation for getting the percent is:
Code:

STATUS=$(( COUNT / TOTAL_VMS * 100 ))
However doing this with bash leads to problems, because COUNT / TOTAL_VMS will produce a fraction, which bash barfs at. Therefore I tend to either use `bc` for this calculation, or rearrange it:
Code:

STATUS=$(( COUNT * 100 / TOTAL_VMS ))
though even that doesn't guarantee success; this would be even better:
Code:

STATUS=$(( ( COUNT * 100 ) / TOTAL_VMS ))
But still, using `bc` is much safer, because of the unwhole numbers.


Correcting that calculation should help you address the percents going up wrong; if you're concerned about how they are printing on the screen, you'll want to look into \b and \r for use with `echo -e` and/or `printf` to bring you back to the beginning of the line and print the next value, so it looks like the number is actually counting up in one place.

But.... Which do you want in the end - a bar, or the % display? If I remember right, there is an already-made tool or script, probably more than one, which can be adapted to doing a progress bar. If you go looking but cannot find it, I'll have a look.

Good luck! :)

EDIT & PS - I also note that you appear to be counting progress, in reverse, from NUM_VMS to zero. If I correctly am seeing that, then you might end up with a percent counter that starts at 100 and goes backward! If so, not a big deal, we can fix that easy enough later.

grail 11-25-2010 05:55 PM

Quote:

((STATUS= 100 / COUNT_VMS ))
STATUS=$(( 100 / COUNT_VMS ))
Actually these are equivalent. What I found interesting though is that you use this construct but later you go with:
Quote:

COUNT_VMS=`echo $COUNT_VMS-1 | bc`
Why not use the same:
Code:

((COUNT_VMS--))
Having had a little further look, maybe something like this could work:
Code:

(( STATUS = 100 / COUNT_VMS ))
(( MODULO = 100 / COUNT_VMS ))
COUNTER=1

for uuid in $ALL_VMS
do
    <do your stuff>
    echo -n "$(((STATUS * COUNTER++)+MODULO))% "
done

This will allow for non-even divisible values and you can play with MODULO in an if assuming you only want the last value (ie 100) to include it.

Quote:

However doing this with bash leads to problems, because COUNT / TOTAL_VMS will produce a fraction, which bash barfs at.
This not quite correct. bash simply delivers the whole number part as opposed to the actual fraction :)

As GGirl has said though, there are plenty of progress bar options around. Maybe have a look at Greg Wooledge's page

genderbender 11-25-2010 06:36 PM

Quote:

Originally Posted by grail (Post 4171262)
What I found interesting though is that you use this construct but later you go with:

Why not use the same:
Code:

((COUNT_VMS--))
Having had a little further look, maybe something like this could work:
[code]
(( STATUS = 100 / COUNT_VMS ))
(( MODULO = 100 / COUNT_VMS ))
COUNTER=1

I used this bit of code purely out of habbit, most of the stuff I write is based on other scripts I've wrote in the past.


Quote:

Originally Posted by grail (Post 4171262)
As GGirl has said though, there are plenty of progress bar options around. Maybe have a look at Greg Wooledge's page

I want to avoid over complicating this, just thought something like echoing a # for each percentage and just overlaying this or something? Donno, I'll take a look at the link but if it's over complicated I'll just stick with %completion; the only reason it's needed is theres no stdout at all so it's impossible to tell how long it's gonna take to 'do my stuff'. If this page hints at using smaller incriments I might be interested (e.g doesnt just go 20/40/60/80/100 each time it's progressed; I'd much rather have 1%+ progression regardless of how much stuff needs doing).

GrapefruiTgirl 11-25-2010 07:18 PM

Quote:

Originally Posted by grail (Post 4171262)
Actually these are equivalent

Once again I learn something new; I have never seen that syntax before.
Quote:

This not quite correct. bash simply delivers the whole number part as opposed to the actual fraction :)
Right, I could have explained that better! The point though that I wanted to get across, is that when doing A/B with bash, and A is always smaller than B, the result is always zero; so calculating something like a % complete, never works right. Not convenient. :)

genderbender 11-26-2010 03:58 AM

Quote:

Originally Posted by grail (Post 4171262)
Having had a little further look, maybe something like this could work:
Code:

(( STATUS = 100 / COUNT_VMS ))
(( MODULO = 100 / COUNT_VMS ))
COUNTER=1

for uuid in $ALL_VMS
do
    <do your stuff>
    echo -n "$(((STATUS * COUNTER++)+MODULO))% "
done


This ones weird... Goes to 112% with 6 values in... If I change the count to 0 it goes up to 96% though. I guess that's close enough :)

grail 11-26-2010 06:33 AM

Doh ... that would be my very bad as I did a typo :(
Change to:
Code:

(( MODULO = 100 % COUNT_VMS ))
This should work now ;)

genderbender 11-26-2010 06:49 AM

Hmmm... starts at 4% and then goes to 84% at the end now :S This is certainly more challenging than I expected. When it's done one task of six it should be at 16% - not 4%. That 'typo' mentioned previously gives better results, I'm gonna have to read this to make sense of it I think :P

grail 11-26-2010 07:26 AM

Well I don't have access to what you are doing so I created a simple for loop and it seems to work as expected, ie for items each is a value of 16 * counter + modulo hence first value
is 20:
Code:

#!/bin/bash

COUNT_VMS=6
(( STATUS = 100 / COUNT_VMS ))
(( MODULO = 100 % COUNT_VMS ))
COUNTER=1

for (( x=1; x<= 6; x++ ))
do
    echo -n "$((STATUS * COUNTER++ + MODULO))% "
done

Output:
Code:

20% 36% 52% 68% 84% 100%

genderbender 11-26-2010 07:34 AM

the counter was still 0.

genderbender 11-26-2010 07:46 AM

Also; my 'bar code' looks like this - it's 90% what I wanted:

Code:

yes "=" | head -n$STATUS | xargs echo -n | tr -d " "
echo -n $(((STATUS * COUNTER++)+MODULO))%

What I could do with is removing the percentages each time whilst moving the percentage forward - what I get when complete is:
Code:

[================20%================36%================52%================68%================84%================100%]
It would be cool if it said 20% and then it dissapeared and printed the next incriment, was playinh with tr to delete numbers and percentages but it crashed, lol.

grail 11-26-2010 09:17 AM

Who's your daddy :) ... Just kidding, this was kinda fun:
Code:

#!/bin/bash

COUNT_VMS=6
(( STATUS = 100 / COUNT_VMS ))
(( MODULO = 100 % COUNT_VMS ))
COUNTER=1

for (( x=1; x<= 6; x++ ))
do
    (( DIFF = 100 - (TOTAL = STATUS * COUNTER++ + MODULO )))
    clear
    line="[$(yes "=" | head -n $TOTAL)$(yes " " | head -n $DIFF)] $TOTAL%"
    echo -n -e "$line" | tr -d "\n"
    sleep 2
done
echo


genderbender 11-26-2010 09:21 AM

That is seriously f*cking cool.

Thanks so much, lol.

This is really awesome, just noticed pressing enter doesn't mess it up, I am seriously impressed, thanks!

grail 11-26-2010 09:35 AM

nah that's cool ... it was fun :)

I think the true aim would be if you could do it without the clear so you could have other data on screen scrolling through, but
for a quick knock up it gets the job done.

Cheers


All times are GMT -5. The time now is 05:15 PM.