[ACCEPTED]-A simple small shell script to compute averages-syntax-error

Accepted answer
Score: 18
 awk '{sum+=$2}END{printf "Sum=%d\nCount=%d\nAve=%.2f\n",sum,NR,sum/NR}' ave.txt

First off, Bash cannot do integer division, you 4 will either need to pipe the math to a tool 3 like 'bc' or just use awk to do it all as 2 it's quite powerful; after all, that entire 1 script of yours was turned into a 1-liner.

Sample Input

$ cat ave.txt
FirstPerson 23
SecondPerson 36
ThirdPerson 22

Result

Sum=81
Count=3
Ave=27.00
Score: 6

I don't know about your shell script, but 7 I know you should be using the right tool 6 for the job. That tool is AWK. It was designed 5 specifically for this task and, if you are 4 using UNIX (or Linux, or Mac OS X or whatever) you 3 have it installed. This is the one-liner:

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

Read 2 the guide for AWK. The philosophy of UNIX is DO NOT REINVENT THE WHEEL. Use the 1 right tools, buddy.

Good luck,

Score: 2

the code below works, you can probably optimize 1 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
Score: 2

try this

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

0

Score: 1

To begin with, you shouldn't have spaces 1 on either side of an =

Score: 1

The error "Unterminated quoted string" is 1 self explanatory

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

Should be

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

By looking at the script there does not 4 seem to be much that you are doing correctly. I 3 recommend looking at some Bash how to and 2 follow simple steps to get it to do what 1 you expect.

  1. no spaces after variable assignment, should be sum= and so on
  2. while [ ! -f $1 ] might actually do something but not what you expect
  3. read -p "Re-enter the filename and hit <Enter>: " definitely does not do what you expect
  4. and so on
Score: 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);
  }     

0

More Related questions