[SOLVED] progress bar instead of % complete in my for loop
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Last edited by GrapefruiTgirl; 11-25-2010 at 11:54 AM.
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
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).
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.
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
Last edited by genderbender; 11-26-2010 at 06:56 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:
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.