OK... several problems:
0. not sure if your sources are this way too; but both files I got contained
"^M"s as though the files originated in a DOS system. I've attached my "winfix"
script to fix files that come from DOS-based systems. BTW, this is one of those
very subtle reasons why you can get "bash: ./<scriptname>: No such file or
directory" when everything else seems OK...
1. egrep is looking for " K" (-i) which does not exist in the data file; hence
the while loop will never execute, resulting in the div_by_0 even when the
script is fixed. I replaced 'egrep -ie " K"' with 'cat' to continue testing.
2. your loop is actually running in a subshell since it is part of a pipeline;
hence, variables are not available to the current script. Another reason I
dislike sh and its variants (incl Perl).
3. you are mixing numeric and textual numbers in a language which is poorly
type-checked; note the use of "-lt" vs "<".
Here is your script with extraneous syntax (which is unnecessary in this
example) and external calls for which thereis a builtin, removed and variable
names which make it clearer; note that it also runs MUCH faster.
#!/bin/sh
TOTAL=0
COUNTED=0
# get the results into our script
# FOR DEBUGGING: #egrep -ie " K"
# bring the results into our script
SIZES=`cat 8.0.listing | gawk -F ' ' '{ print $5 }'`
for SIZE in $SIZES
do
[ ${#SIZE} -lt 7 ] && {
let TOTAL+=$SIZE
let COUNTED+=1
}
done
echo "Total $TOTAL in $COUNTED records"
# Check for div_by_0
[ $COUNTED ] && {
let AVG=$TOTAL/$COUNTED
echo "Average counted size is $AVG"
}
HTH,
Pierre
SoloCDM wrote:
>
> Two files are attached. k-s.sample is the program and 8.0.listing is
> the data file. The following is the outcome:
>
> Total 0
> ./k-s.sample: 0/0 : division by 0 (error token is " ")
>
> If I don't initialize the variables, nothing is the outcome. The
> echo-s prove the data is calculating.
>
> Note: When you reply to this message, please include the mailing
> list/newsgroup address and my email address in To:.
>
> *********************************************************************
> Signed,
> SoloCDM
>
> --------------------------------------------------------------------------------
> Name: k-s.sample
> k-s.sample Type: Plain Text (TEXT/PLAIN)
> Encoding: BASE64
>
> Name: 8.0.listing
> 8.0.listing Type: Plain Text (TEXT/PLAIN)
> Encoding: BASE64
--
Support Linux development: http://www.linux-mandrake.com/donations/
Last reboot reason: 01/03/27: winter storm 6hr power outage
#!/bin/sh
mv $1 $1.orig
tr -d "\r" < $1.orig > $1