Simple little shell script for calculating averages

Can someone tell me what I'm doing wrong here?

#!/bin/sh

if [ $# = 0 ]
then
    echo "Usage: $0 <filename>"
    exit 1
fi

sum=0
count=0

while [ $0 != 0 ]
do
        sum="$sum"+"$2"
        count="$count"+ 1

done
if [ "$count" != 0 ]
then
        avg="$sum"/"$count"
        printf "Sum= $sum \n Count= $count  \n Avg= $avg"
        exit 0
else
        printf "Sum= $sum \n Count= $count  \n Avg= undefined"
        exit 0
fi
exit 1

Here is the output when I try to check the code:

./average

sum: =: No such file or directory
sum: 0: No such file or directory
./average: 11: count: not found
[: 18: !=0: unexpected operator
./average: 25: Syntax error: Unterminated quoted string

Basically, if I had a file that looked something like this:

FirstPerson 23 

SecondPerson 36

ThirdPerson 22

I want to be able to read this in my program and output it:

Sum = FirstPerson+SecondPerson+ThirdPerson

Count = NumberofPeople

Average = Sum/Count
+3
source share
8 answers

the code below works, you can optimize it if you want (or use awk, perl, etc.):

#!/bin/bash

if [ $# -ne 1 ]; then
        echo "Usage: \"$0\" <filename>"
        exit
fi

if [ ! -f $1 ]; then
        echo "$1 file not found."
        echo "Usage: $0 <filename>"
        exit
fi

sum=0
count=0
arq=$1

while read line
do
        num=`echo ${line#* }`
        sum=`expr $sum + $num`
        count=`expr $count + 1`
done < "$arq"

if [ "$count" != 0 ]
then
        avg=`expr $sum / $count`
        printf "Sum= \"$sum\" \n Count= \"$count\"  \n Avg= \"$avg\""
        exit 0
else
        printf "Sum= \"$sum\" \n Count= \"$count\"  \n Avg= undefined"
        exit 0
fi
+2
source
 awk '{sum+=$2}END{printf "Sum=%d\nCount=%d\nAve=%.2f\n",sum,NR,sum/NR}' ave.txt

-, Bash , , , "bc", awk, , ; , script 1-.

$ cat ave.txt
FirstPerson 23
SecondPerson 36
ThirdPerson 22

Sum=81
Count=3
Ave=27.00
+15

script, , . AWK. , UNIX ( Linux, Mac OS X - ), . :

awk '{ sum+=$2; count+=1 } END {print "Sum =",sum; print "Count =",count; print "Average= ",sum/count}' test2.dat 

AWK. UNIX . , .

,

+5

count_ppl=0
sum=0
while read a b
do
   sum=$((sum+b))
   count_ppl=$((count_ppl+1))
done < file
echo "Sum=$sum"
echo "Count=$count_ppl"
avg=$(echo "scale=2;$sum/$count_ppl" | bc)
echo "Average=" $avg
+2

=

+1

"Unterminated quoted string"

printf "Sum= \"$sum\" \n Count= \"$count\"  \n Avg= "\$avg\""

printf "Sum= \"$sum\" \n Count= \"$count\"  \n Avg= \"$avg\""
+1

script, , . Bash , , .

  • sum= ..
  • while [ ! -f $1 ] - , ,
  • read -p "Re-enter the filename and hit <Enter>: " ,
  • ..
+1
            c program for simple interest

 #include<stdio.h>
 int main(void)
  { 
    int p,r,t,s;
    printf("enter the principle value");
    scanf("%d",&p);
    printf("enter the rate or interest");
    scanf("%d",&r);
    printf("enter the time period ");
    scanf("%d",&t);
    s=p*t*r/100;
    printf("the simple interest is %d",&s);
  }     
+1

Source: https://habr.com/ru/post/1725370/


All Articles