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

发布于 2024-08-15 06:16:25 字数 935 浏览 4 评论 0原文

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

#!/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

眼泪都笑了 2024-08-22 06:16:25
 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
 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 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

$ cat ave.txt
FirstPerson 23
SecondPerson 36
ThirdPerson 22

Result

Sum=81
Count=3
Ave=27.00
倾城泪 2024-08-22 06:16:25

我不知道你的 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 

阅读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:

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

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

Good luck,

别闹i 2024-08-22 06:16:25

下面的代码可以工作,如果你愿意,你可以优化它(或使用 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

the code below works, you can probably optimize 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
别想她 2024-08-22 06:16:25

试试这个

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

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
二智少女 2024-08-22 06:16:25

首先,= 两边不应该有空格

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

怂人 2024-08-22 06:16:25

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

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

应该是

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

The error "Unterminated quoted string" is self explanatory

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

Should be

printf "Sum= \"$sum\" \n Count= \"$count\"  \n Avg= \"$avg\""
少女的英雄梦 2024-08-22 06:16:25

通过查看脚本,您似乎没有做太多正确的事情。
我建议查看一些 Bash 操作方法并遵循简单的步骤来让它完成您期望的操作。

  1. 变量赋值后没有空格,应该是 sum= 等等
  2. while [ ! -f $1 ] 实际上可能会做一些事情,但不是您期望的
  3. read -p "Re-enter the filename and hit: " 绝对不会做您期望的事情
  4. ,所以在

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.

  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
似最初 2024-08-22 06:16:25
            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);
  }     
            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);
  }     
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文