谁能告诉我我在这里做错了什么?
#!/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