LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-25-2010, 11:16 AM   #1
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Rep: Reputation: 31
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

Last edited by genderbender; 11-26-2010 at 06:56 AM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 11-25-2010, 11:21 AM   #2
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
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?
 
Old 11-25-2010, 11:48 AM   #3
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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.
 
1 members found this post helpful.
Old 11-25-2010, 05:55 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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
 
2 members found this post helpful.
Old 11-25-2010, 06:36 PM   #5
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by grail View Post
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 View Post
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).
 
Old 11-25-2010, 07:18 PM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Quote:
Originally Posted by grail View Post
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.
 
Old 11-26-2010, 03:58 AM   #7
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by grail View Post
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
 
Old 11-26-2010, 06:33 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Doh ... that would be my very bad as I did a typo
Change to:
Code:
(( MODULO = 100 % COUNT_VMS ))
This should work now
 
Old 11-26-2010, 06:49 AM   #9
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
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.
 
Old 11-26-2010, 07:26 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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%
 
1 members found this post helpful.
Old 11-26-2010, 07:34 AM   #11
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
the counter was still 0.
 
Old 11-26-2010, 07:46 AM   #12
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
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.
 
Old 11-26-2010, 09:17 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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
 
1 members found this post helpful.
Old 11-26-2010, 09:21 AM   #14
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
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!

Last edited by genderbender; 11-26-2010 at 09:22 AM.
 
Old 11-26-2010, 09:35 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mv with progress bar gbowden Linux - General 28 08-14-2013 04:42 AM
cp progress bar edwardsiow Linux - General 26 09-04-2011 08:53 AM
cp with progress bar? Rotwang Linux - General 5 04-27-2005 07:49 PM
cp: progress bar chii-chan Linux - General 2 10-30-2003 06:30 PM
Progress Bar zael Programming 3 10-01-2003 12:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration