一个简单的小shell脚本来计算平均值

时间:2009-12-11 06:18:00

标签: bash shell logic syntax-error

谁能告诉我这里我做错了什么?

#!/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

这是我尝试测试代码时的输出:

./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

基本上,如果我有一个看起来像这样的文件:

FirstPerson 23 

SecondPerson 36

ThirdPerson 22

我希望能够将其读入我的程序并输出:

Sum = FirstPerson+SecondPerson+ThirdPerson

Count = NumberofPeople

Average = Sum/Count

8 个答案:

答案 0 :(得分:16)

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

首先,Bash不能进行整数除法,你需要将数学管道传递给像'bc'这样的工具,或者只是使用awk来完成所有这一切,因为它非常强大;毕竟,你的整个剧本都变成了一个单行。

示例输入

$ cat ave.txt
FirstPerson 23
SecondPerson 36
ThirdPerson 22

结果

Sum=81
Count=3
Ave=27.00

答案 1 :(得分:6)

我不知道你的shell脚本,但我知道你应该使用正确的工具来完成工作。该工具是AWK。它专为此任务而设计,如果您使用的是UNIX(或Linux,或Mac OS X或其他),则可以安装它。这是单线:

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

阅读guide for AWK。 UNIX的理念是不要重新使用轮子。使用合适的工具,好友。

祝你好运,

答案 2 :(得分:2)

下面的代码有效,你可以根据需要优化它(或使用awk,perl等):

#!/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

答案 3 :(得分:2)

试试这个

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

答案 4 :(得分:1)

首先,你不应该在=

的两边都有空格

答案 5 :(得分:1)

错误“未终止的引用字符串”是不言自明的

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

应该是

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

答案 6 :(得分:1)

通过查看脚本,似乎没有太多你正确的做法。 我建议看一些Bash如何操作,并按照简单的步骤让它做你期望的事。

  1. 变量赋值后没有空格,应该是sum=等等
  2. while [ ! -f $1 ]实际上可能会做一些事情而不是你期望的事情
  3. read -p "Re-enter the filename and hit <Enter>: "肯定没有达到预期目标

答案 7 :(得分: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);
  }