From: Haight Ash on
Trying to teach myself some shell scripting. I seem to of set up this
while loop incorrectly, it never exits.

What I'm trying to do is"
1) supply a number which sets the number of times it prompts for a
number (ex: How many values do you want to enter)
2) loop for X number of times depending on #1
3) keep track of min and max values entered
3) add the sum of all values
4) average the values

any pointers would be appreciated

#!/usr/local/bin/bash

minvalue="999999"
maxvalue=-999999
sumvalue=0
counter=0
numv=0

echo -n "Enter the number of values to be input: "; read numv;

while [ $counter < $numv ]
do
echo -n "Enter a value: "; read value;
let sumvalue="$sumvalue+$value"
if [ $value -lt $minvalue ]; then
let minvalue="$value"
fi
if [ $value -gt $maxvalue ]; then
let maxvalue="$value"
fi
let counter="$counter+1"
echo $counter
done

let averagevalue="$sumvalue / $numv"

echo "total = $sumvalue"
echo "minimum val = $minvalue"
echo "average value = $averagevalue"
echo "maximum value = $maxvalue"
From: David W. Hodgins on
On Sat, 06 Feb 2010 17:08:26 -0500, Haight Ash <ksk415(a)gmail.com> wrote:

> Trying to teach myself some shell scripting. I seem to of set up this
> while loop incorrectly, it never exits.

What version of bash are you using?

Here, I'm using ...
$ bash --version
GNU bash, version 4.0.33(2)-release

> while [ $counter < $numv ]

The < character is being interpreted to mean "pipe what follows it
to stdin". Change the line to ...

while [ "$counter" -lt "$numv" ]

The script then works here. Good idea to get into the habit
of always quoting the variables, unless you have a good reason
not to.

Check out http://tldp.org/LDP/abs/html/

Regards, Dave Hodgins

--
Change nomail.afraid.org to ody.ca to reply by email.
(nomail.afraid.org has been set up specifically for
use in usenet. Feel free to use it yourself.)
From: Bit Twister on
On Sat, 6 Feb 2010 14:08:26 -0800 (PST), Haight Ash wrote:
> Trying to teach myself some shell scripting. I seem to of set up this
> while loop incorrectly, it never exits.

> any pointers would be appreciated

The "expression" is incorrect

> while [ $counter < $numv ]

You may want to bookmark/read http://tldp.org/LDP/abs/html/index.html

For a running start on your problem
http://tldp.org/LDP/abs/html/comparison-ops.html

For quick down and dirty lookup, try

man test

From: Haight Ash on

> What version of bash are you using?
>
> Here, I'm using ...
> $ bash --version
> GNU bash, version 4.0.33(2)-release
>
> > while [ $counter < $numv ]
>
> The < character is being interpreted to mean "pipe what follows it
> to stdin".  Change the line to ...
>
> while [ "$counter" -lt "$numv" ]
>
> The script then works here.  Good idea to get into the habit
> of always quoting the variables, unless you have a good reason
> not to.
>
> Check outhttp://tldp.org/LDP/abs/html/
>
> Regards, Dave Hodgins


Thanks! that did the trick. also, thanks for the tip on quoting
variables.
From: Ed Morton on
On 2/6/2010 4:08 PM, Haight Ash wrote:
> Trying to teach myself some shell scripting. I seem to of set up this
> while loop incorrectly, it never exits.
>
> What I'm trying to do is"
> 1) supply a number which sets the number of times it prompts for a
> number (ex: How many values do you want to enter)
> 2) loop for X number of times depending on #1
> 3) keep track of min and max values entered
> 3) add the sum of all values
> 4) average the values
>
> any pointers would be appreciated
>
> #!/usr/local/bin/bash
>
> minvalue="999999"
> maxvalue=-999999
> sumvalue=0
> counter=0
> numv=0
>
> echo -n "Enter the number of values to be input: "; read numv;
>
> while [ $counter< $numv ]
> do
> echo -n "Enter a value: "; read value;
> let sumvalue="$sumvalue+$value"
> if [ $value -lt $minvalue ]; then
> let minvalue="$value"
> fi
> if [ $value -gt $maxvalue ]; then
> let maxvalue="$value"
> fi
> let counter="$counter+1"
> echo $counter
> done
>
> let averagevalue="$sumvalue / $numv"
>
> echo "total = $sumvalue"
> echo "minimum val = $minvalue"
> echo "average value = $averagevalue"
> echo "maximum value = $maxvalue"

What's all this "let" stuff we see showing up in scripts these days? It's like
being back in the Basic era!

minvalue=999999
maxvalue=-999999
sumvalue=0
counter=0
numv=0

printf "Enter the number of values to be input: "; read numv;

while (( counter < numv ))
do
printf "Enter a value: "; read value;
sumvalue=$(( sumvalue + value ))
if (( value < minvalue )); then
minvalue="$value"
fi
if (( value > maxvalue )); then
maxvalue="$value"
fi
counter=$(( counter+1 ))
printf "%d\n" "$counter"
done

averagevalue=$(( sumvalue / numv ))

printf "total = %d\n "$sumvalue"
printf "minimum val = %d\n" "$minvalue"
printf "average value = %d\n "$averagevalue"
printf "maximum value = %d\n" "$maxvalue"

Note that in general if you find yourself writing a loop in a shell script, it's
the wrong approach.

Regards,

Ed.