I’m trying to create the program to read each lines and calculate each line’s average and store into array…For example, program will read first line, add all the numbers and divide by 24 to calculate the average which will be stored into Avg_list[1].
When I try to run the program, I encounter following error, I have no idea why it doesn’t work… Can someone identify the problem?
Code: in Ksh
c=0
while read -r line ; do
v=$line
set -- $v ((g=($2+$3+$4+$5+$6+$7+$8+$9+$10+$11+$12+$13+$14+$15+$16+$17+$18+$19+$20+$21+$22+$23+$24+$25+$26)/24))
echo $g
Avg_list[${c}]=$g
((c=c+1))
done < daily.txt
daily.txt
CPU 55 54 54 54 54 54 54 54 54 54 54 54 54 54 54 55 54 54 55 56 57 54 57 54
CPEAK 56 56 57 55 58 56 56 56 57 55 60 56 55 56 55 56 58 55 57 56 63 56 72 57
RAM 97 97 97 97 97 96 96 96 96 96 96 93 91 89 86 84 90 90 95 97 97 97 97 97
RPEAK 97 97 97 97 97 97 96 96 96 96 96 96 92 90 91 81 94 89 97 97 97 97 97 97
Error note:
while read -r line ; do
v=$line
set -- $v
((g=($2+$3+$4+$5+$6+$7+$8+$9+$10+$11+$12+$13+$14+$15+$16+$17+$18+$19+$20+$21+$22+$23+$24+$25+$26)/24))
echo $g
Avg_list[${c}]=$g
((c=c+1))
done < daily.txt
+ 0< daily.txt
+ read -r line
+ v=CPU 54 54 54 54 54 54 54 54 54 54 54 54 54 54 55 54 54 55 56 57 54 57 54 54
+ set -- CPU 54 54 54 54 54 54 54 54 54 54 54 54 54 54 55 54 54 55 56 57 54 57 54 54
+ (( g=(54+54+54+54+54+54+54+54+CPU0+CPU1+CPU2+CPU3+CPU4+CPU5+CPU6+CPU7+CPU8+CPU9+540+541+542+543+544+545+546)/24 ))
PerformanceAM.sh[21]: g=(54+54+54+54+54+54+54+54+CPU0+CPU1+CPU2+CPU3+CPU4+CPU5+CPU6+CPU7+CPU8+CPU9+540+541+542+543+544+545+546)/24: 0403-009 The specified number is not valid for this command.
EDIT
while read -r line ; do
v=$line
set -- $v
((g=${2}+${3}+${4}+${5}+${6}+${7}+${8}+${9}+${10}+${11}+${12}+${13}+${14}+${15}+${16}+${17}+${18}+${19}+${20}+${21}+${22}+${23}+${24}+${25}+${26})/24))
echo $g
Avg_list[${c}]=$g
((c=c+1))
done < daily.txt
New error:
while read -r line ; do
v=$line
set — $v
((g=${2}+${3}+${4}+${5}+${6}+${7}+${8}+${9}+${10}+${11}+${12}+${13}+${14}+${15}+${16}+${17}+${18}+${19}+${20}+${21}+${22}+${23}+${24}+${25}+${26})/24)PerformanceAM.sh[18]: 0403-057 Syntax error at line 21 : `/24′ is not expected.
Thanks for your suggestions! when I tried used bracket i get this error…I’m now even more confused….it seems like it’s not collecting numbers at all…
You were missing one
(atg=${2}and the arguments only go until ${25} not ${26}.