From: David Combs on
In article <hkmpgv$uvu$1(a)news.eternal-september.org>,
Ed Morton <mortonspam(a)gmail.com> wrote:


snip

>
>FWIW, here's how I'd really write a script to calculate the min/max/ave from a
>set of values being input by a user when prompted:
>
>$ cat tst.sh
>awk 'BEGIN{ sum=min=max=ave="NAN"; printf "Enter a value: " }
>{
> sum += $0
> min = ( ($0 < min) || (min == "NAN") ? $0 : min)
> max = ( ($0 > max) || (max == "NAN") ? $0 : max)
> printf "Enter a value: "
>}
>END {
> ave = ( NR ? sum / NR : ave )
> print "\ntotal =", sum
> print "minimum value =", min
> print "average value =", ave
> print "maximum value =", max
>}'
>$
>$ ./tst.sh
>Enter a value: 1
>Enter a value: 2
>Enter a value:
>total = 3
>minimum value = 1
>average value = 1.5
>maximum value = 2
>$
>$ ./tst.sh
>Enter a value:
>total = NAN
>minimum value = NAN
>average value = NAN
>maximum value = NAN

Try it again, the 2nd group, say, and then as the 4th item
enter a NAN, and maybe a 5th as 100 -- whatcha get?

What is the user told will happen when he types
in a "NAN"?

Ignore that entry, forget it even happened?

Abort the whole run thus far by assigning every variable
a NAN, thus losing the info you'd built up thus far?


And then what -- since your initial values were each
NAN, keep reading numbers or NAN's, giving him as
result whatever you've computed from the most recent
run of non-NAN values?

Maybe it'd be better to check first for a NAN, and just
"continue" if that's what you see -- working out for
the user as "the principle of least surprise"?

I'm probably wrong somewhere in there, but you
can see what I'm trying to get at.


David


From: Janis Papanagnou on
David Combs wrote:
> In article <hkl7sf$ipv$1(a)news.eternal-september.org>,
> Ed Morton <mortonspam(a)gmail.com> wrote:
> ...
> ...
>
>> printf "Enter the number of values to be input: "; read numv;
>>
>> while (( counter < numv ))
> ^ -----^---- The extra paren-pair "hides" the "<"
> from being interpreted as a redirection? (Looks like it would.)

The (( ... )) construct is an arithmetic command. Within the double
brackets you can write math expressions as you would expect, incusive
using < > <= >= == != for comparison or using variables without the
explicit dereferencing using $. If using it as a condition in if/while
constructs it evaluates to true if the arithmetic expression results
in non-zero, otherwise false.

Janis

>
> Not that I know bash ("let"?) -- as bool, does "<" work
> on numbers, or must he use ".lt."?
>
> Silly questions -- maybe those "lets" gave me a concussion!
>
>
> David
>
>