一个简单的小 shell 脚本来计算平均值
谁能告诉我我在这里做错了什么?
#!/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
Can anyone 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's the output when I try to test 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 that into my program and have it output:
Sum = FirstPerson+SecondPerson+ThirdPerson
Count = NumberofPeople
Average = Sum/Count
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
首先,Bash 不能进行整数除法,您要么需要将数学通过管道传输到像“bc”这样的工具,要么只使用 awk 来完成这一切,因为它非常强大;毕竟,你的整个剧本都变成了一句台词。
输入
结果示例
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.
Sample Input
Result
我不知道你的 shell 脚本,但我知道你应该使用正确的工具来完成这项工作。这个工具就是AWK。它是专门为此任务而设计的,如果您使用的是 UNIX(或 Linux、Mac OS X 或其他),则您已经安装了它。这是一句话:
阅读AWK 指南。 UNIX 的哲学是不要重新发明轮子。使用正确的工具,伙计。
祝你好运,
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:
Read the guide for AWK. The philosophy of UNIX is DO NOT REINVENT THE WHEEL. Use the right tools, buddy.
Good luck,
下面的代码可以工作,如果你愿意,你可以优化它(或使用 awk、perl 等):
the code below works, you can probably optimize it if you want (or use awk, perl, etc):
试试这个
try this
首先,= 两边不应该有空格
To begin with, you shouldn't have spaces on either side of an =
错误“未终止的引号字符串”是不言自明的
应该是
The error "Unterminated quoted string" is self explanatory
Should be
通过查看脚本,您似乎没有做太多正确的事情。
我建议查看一些 Bash 操作方法并遵循简单的步骤来让它完成您期望的操作。
sum=
等等while [ ! -f $1 ]
实际上可能会做一些事情,但不是您期望的read -p "Re-enter the filename and hit: "
绝对不会做您期望的事情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.
sum=
and so onwhile [ ! -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