zsh-users
 help / color / mirror / code / Atom feed
* math, percentage
@ 2005-12-31  6:56 Brian K. White
  2005-12-31  7:22 ` Eric Mangold
  2005-12-31  7:25 ` Dan Nelson
  0 siblings, 2 replies; 6+ messages in thread
From: Brian K. White @ 2005-12-31  6:56 UTC (permalink / raw)
  To: zsh-users

is there a way to do this with built in math instead of running bc ?

tp=`echo "scale=0;(${t}*100)/$T" |bc`

T = some dynamic total number like `ls |wc -l`
t = counter that starts at 1 and incriments as a loop walks through T
tp = percent of total job completed so far

Thanks
Brian K. White  --  brian@aljex.com  --  http://www.aljex.com/bkw/
+++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++.
filePro  BBx    Linux  SCO  FreeBSD    #callahans  Satriani  Filk!


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: math, percentage
  2005-12-31  6:56 math, percentage Brian K. White
@ 2005-12-31  7:22 ` Eric Mangold
  2005-12-31  7:25 ` Dan Nelson
  1 sibling, 0 replies; 6+ messages in thread
From: Eric Mangold @ 2005-12-31  7:22 UTC (permalink / raw)
  To: Brian K. White, zsh-users

Here are some ideas:

total=15
typeset -F 2 percentage # only show two decimal places when formatting
for i in {1..$total}; {
     print $i of $total
     percentage=$(( $i.0 / $total * 100 ))
     print percentage $percentage
}

On Sat, 31 Dec 2005 17:56:35 +1100, Brian K. White <brian@aljex.com> wrote:

> is there a way to do this with built in math instead of running bc ?
>
> tp=`echo "scale=0;(${t}*100)/$T" |bc`
>
> T = some dynamic total number like `ls |wc -l`
> t = counter that starts at 1 and incriments as a loop walks through T
> tp = percent of total job completed so far
>
> Thanks
> Brian K. White  --  brian@aljex.com  --  http://www.aljex.com/bkw/
> +++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++.
> filePro  BBx    Linux  SCO  FreeBSD    #callahans  Satriani  Filk!
>



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: math, percentage
  2005-12-31  6:56 math, percentage Brian K. White
  2005-12-31  7:22 ` Eric Mangold
@ 2005-12-31  7:25 ` Dan Nelson
  2005-12-31 20:44   ` Brian K. White
  1 sibling, 1 reply; 6+ messages in thread
From: Dan Nelson @ 2005-12-31  7:25 UTC (permalink / raw)
  To: Brian K. White; +Cc: zsh-users

In the last episode (Dec 31), Brian K. White said:
> is there a way to do this with built in math instead of running bc ?
> 
> tp=`echo "scale=0;(${t}*100)/$T" |bc`
> 
> T = some dynamic total number like `ls |wc -l`
> t = counter that starts at 1 and incriments as a loop walks through T
> tp = percent of total job completed so far

(( tp=t*100/T ))

-- 
	Dan Nelson
	dnelson@allantgroup.com


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: math, percentage
  2005-12-31  7:25 ` Dan Nelson
@ 2005-12-31 20:44   ` Brian K. White
       [not found]     ` <488030720601041505g7a9aee72nc8ebcf1bde13b3a4@mail.gmail.com>
  0 siblings, 1 reply; 6+ messages in thread
From: Brian K. White @ 2005-12-31 20:44 UTC (permalink / raw)
  To: zsh-users


----- Original Message ----- 
From: "Dan Nelson" <dnelson@allantgroup.com>
To: "Brian K. White" <brian@aljex.com>
Cc: <zsh-users@sunsite.dk>
Sent: Saturday, December 31, 2005 2:25 AM
Subject: Re: math, percentage


> In the last episode (Dec 31), Brian K. White said:
>> is there a way to do this with built in math instead of running bc ?
>>
>> tp=`echo "scale=0;(${t}*100)/$T" |bc`
>>
>> T = some dynamic total number like `ls |wc -l`
>> t = counter that starts at 1 and incriments as a loop walks through T
>> tp = percent of total job completed so far
>
> (( tp=t*100/T ))

Nice.
Never knew you could do the assignment inside the braces like that,
but even so I should have thought to at least try tp=$((t*100/T)) since I 
use n=$((n+1)) a hundred times a day.

thanks
Brian K. White  --  brian@aljex.com  --  http://www.aljex.com/bkw/
+++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++.
filePro  BBx    Linux  SCO  FreeBSD    #callahans  Satriani  Filk!


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: math, percentage
       [not found]     ` <488030720601041505g7a9aee72nc8ebcf1bde13b3a4@mail.gmail.com>
@ 2006-01-05  4:43       ` Brian K. White
  2006-01-05 23:54         ` Jonathan Hankins
  0 siblings, 1 reply; 6+ messages in thread
From: Brian K. White @ 2006-01-05  4:43 UTC (permalink / raw)
  To: zsh-users


----- Original Message ----- 
From: "John Reese" <john.reese@gmail.com>
To: "Brian K. White" <brian@aljex.com>
Sent: Wednesday, January 04, 2006 6:05 PM
Subject: Re: math, percentage


> Nice.
> Never knew you could do the assignment inside the braces like that,
> but even so I should have thought to at least try tp=$((t*100/T)) since I
> use n=$((n+1)) a hundred times a day.
>
> thanks
> Brian K. White  --  brian@aljex.com  --  http://www.aljex.com/bkw/

You could do
((n++))
instead and save three hundred characters a day.

----

Ah, neat. :)

It should be noted though:

Up to to this point the examples also work in ksh88 ,the version of ksh 
still shipping stock with many commercial unii as /bin/ksh.

((n++)) does not work in ksh88.
It does work in ksh93, bash, pdksh, and of course zsh.

Most of my fancier scripts need to explicitly call a particular shell anways 
because of special features, so this is useful.

Brian K. White  --  brian@aljex.com  --  http://www.aljex.com/bkw/
+++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++.
filePro  BBx    Linux  SCO  FreeBSD    #callahans  Satriani  Filk!


-----
<off topic here but it should be explained why ksh93 doesn't obviate ksh88 
and thereby the portability comment>

The existence and availability of ksh93 for most platforms, even though it 
even ships stock with a few very recent ones (but still not as /bin/ksh), 
must not be considered as the simple answer for anything that doesn't work 
in ksh88 though because it's a bit buggy on some systems and cannot be used 
as the drop in replacement for the stock sh or ksh or as roots or even a 
users login shell. Ask me how I know. :) It seems ok at first but after a 
few days in production with a couple hundred users you realize you are 
having a lot of scattered problems you didn't used to have, and putting the 
old binary back ends the trend.

It's useful enough though to install it somewhere and call it explicitly on 
the bang line. My biggest item about ksh93 lately is that it has a built-in 
sleep that takes floating point values. I have things that really do want a 
"sleep .1", and anything that wants that, by definition also really wants it 
built-in, not to fork a child and load an executable every time. No other 
shell anywhere has that that I've found. (hint!)
Perl has nap() and probably other popular non bourne scripting languages 
have some form of usleep or sleep .xx but heck, so what? So does C.
Also there is:
    VARS="look at all these assignments woohoo read is cool how many lines 
of code would this be without it I ask you montoyo inigra"
    grep "^${LOGNAME}:" /path/to/userdefs | IFS=: read junk $VARS
    export $VARS
    echo $woohoo

The bugginess of ksh93 on the system above is at least partly due to the 
at&t build system for ksh that is such a pain in the ___ that hardly anyone 
even attempts to build it, and fewer succeed. For SCO Open Server 5.x.x and 
lower, there is exactly one binary out there, made by one guy a few years 
ago, and it's a little buggy, and he knew it all along, and he's not 
attempting to play with it again. I tried but ran into problems that I 
couldn't debug because the convoluted build system hid and buried the real 
problem too well and I actually have a job that doesn't allow me a couple of 
solid weeks just to build a new shell. :) So it's possible that ksh93 itself 
is fine and merely it's difficult to do a thorough job of porting it. Either 
way, it still means you can't count on it.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: math, percentage
  2006-01-05  4:43       ` Brian K. White
@ 2006-01-05 23:54         ` Jonathan Hankins
  0 siblings, 0 replies; 6+ messages in thread
From: Jonathan Hankins @ 2006-01-05 23:54 UTC (permalink / raw)
  To: zsh-users

"Brian K. White" <brian@aljex.com> writes:

> explicitly on the bang line. My biggest item about ksh93 lately is
> that it has a built-in sleep that takes floating point values. I have
> things that really do want a "sleep .1", and anything that wants that,
> by definition also really wants it built-in, not to fork a child and
> load an executable every time. No other shell anywhere has that that
> I've found. (hint!)  Perl has nap() and probably other popular non

According to zshmodules(1), you can use zselect to sleep with
approximately 1/100 sec. granulatiry.

typeset -F SECONDS
zmodload -i zsh/zselect
print $SECONDS
# sleep for 1/5 sec
zselect -t 20
print $SECONDS

On my system, this gives:

922.1230270000
922.3312820000

-Jonathan Hankins

-- 
+------------------+-----------------------------------------------------+
|Jonathan Hankins  | 		       	 jonathan-hankins@mindspring.com |
+------------------+-----------------------------------------------------+


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-01-05 23:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-12-31  6:56 math, percentage Brian K. White
2005-12-31  7:22 ` Eric Mangold
2005-12-31  7:25 ` Dan Nelson
2005-12-31 20:44   ` Brian K. White
     [not found]     ` <488030720601041505g7a9aee72nc8ebcf1bde13b3a4@mail.gmail.com>
2006-01-05  4:43       ` Brian K. White
2006-01-05 23:54         ` Jonathan Hankins

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).