bash 一个简单的小shell脚本来计算平均值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1886157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A simple small shell script to compute averages
提问by ???
Can anyone tell me what I'm doing wrong here?
谁能告诉我我在这里做错了什么?
#!/bin/sh
if [ $# = 0 ]
then
echo "Usage: ./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
<filename>"
exit 1
fi
sum=0
count=0
while [ FirstPerson 23
SecondPerson 36
ThirdPerson 22
!= 0 ]
do
sum="$sum"+""
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's the output when I try to test the code:
这是我尝试测试代码时的输出:
Sum = FirstPerson+SecondPerson+ThirdPerson
Count = NumberofPeople
Average = Sum/Count
Basically if I had a file that looked something like this:
基本上,如果我有一个看起来像这样的文件:
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: \" awk '{sum+=}END{printf "Sum=%d\nCount=%d\nAve=%.2f\n",sum,NR,sum/NR}' ave.txt
\" <filename>"
exit
fi
if [ ! -f ]; then
echo " file not found."
echo "Usage: $ cat ave.txt
FirstPerson 23
SecondPerson 36
ThirdPerson 22
<filename>"
exit
fi
sum=0
count=0
arq=
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
I want to be able to read that into my program and have it output:
我希望能够将其读入我的程序并输出:
Sum=81 Count=3 Ave=27.00
采纳答案by Peter Carrero
the code below works, you can probably optimize it if you want (or use awk, perl, etc):
下面的代码有效,你可以根据需要对其进行优化(或使用 awk、perl 等):
awk '{ sum+=; count+=1 } END {print "Sum =",sum; print "Count =",count; print "Average= ",sum/count}' test2.dat
回答by SiegeX
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
First off, Bash cannot do integer division, you will either need to pipe the math to a tool like 'bc' or just use awk to do it all as it's quite powerful; after all, that entire script of yours was turned into a 1-liner.
首先,Bash 不能进行整数除法,你要么需要将数学传递给像 'bc' 这样的工具,或者只是使用 awk 来完成这一切,因为它非常强大;毕竟,您的整个脚本都变成了 1-liner。
Sample Input
样本输入
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);
}
Result
结果
printf "Sum= \"$sum\" \n Count= \"$count\" \n Avg= "$avg\""
回答by Escualo
I don't know about your shell script, but I know you should be using the right tool for the job. That tool is AWK. It was designed specifically for this task and, if you are using UNIX (or Linux, or Mac OS X or whatever) you have it installed. This is the one-liner:
我不知道你的 shell 脚本,但我知道你应该使用正确的工具来完成这项工作。那个工具就是AWK。它是专门为此任务设计的,如果您使用的是 UNIX(或 Linux,或 Mac OS X 或其他),则已安装它。这是单线:
printf "Sum= \"$sum\" \n Count= \"$count\" \n Avg= \"$avg\""
Read the guide for AWK. The philosophy of UNIX is DO NOT REINVENT THE WHEEL. Use the right tools, buddy.
阅读AWK 指南。UNIX 的理念是不要重新发明轮子。使用正确的工具,伙计。
Good luck,
祝你好运,
回答by ghostdog74
try this
尝试这个
##代码##回答by ghostdog74
回答by pavium
To begin with, you shouldn't have spaces on either side of an =
首先,你不应该在 = 的两边有空格
回答by Anand Shah
The error "Unterminated quoted string" is self explanatory
错误“未终止的引用字符串”是不言自明的
##代码##Should be
应该
##代码##回答by stefanB
By looking at the script there does not seem to be much that you are doing correctly. I recommend looking at some Bash how to and follow simple steps to get it to do what you expect.
通过查看脚本,您似乎没有做很多正确的事情。我建议您查看一些 Bash 操作方法,并按照简单的步骤使其按照您的预期运行。
- no spaces after variable assignment, should be
sum=
and so on while [ ! -f $1 ]
might actually do something but not what you expectread -p "Re-enter the filename and hit <Enter>: "
definitely does not do what you expect- and so on
- 变量赋值后没有空格,应该是
sum=
等等 while [ ! -f $1 ]
实际上可能会做一些事情,但不是你期望的read -p "Re-enter the filename and hit <Enter>: "
绝对不会做你期望的- 等等