2016-11-03 61 views
0

我试图让这个bash的总平均...更多

Enter Incidents 
Mon 14 
Tue 5 
Wed 12 

Incidents 

Mon 14 
Tue 5 
Wed 12 

Total 38 

Average 7.6 
------------------------ 

Mon incidents is the highest 11 
Tue incidents is the lowest 2 
------------------------ 

Enter a Day that you'd like to see how many incidents occurred? Wed 
12 Incidents 
------------------------ 

到目前为止,我做了总平均,最大值和最小值。

的问题是“周一如何输出最高11,我能找到的最小和最大,但不能打印出当天最大和最小的值匹配....

#!/bin/bash 

echo " ----Enter Incidents-----" 



echo -n "Mon= " 
read days[0] 


echo -n "Tue= " 
read days[1] 


echo -n "Wed= " 
read days[2] 






echo " ------Incidents--------" 


total=`expr ${days[0]} + ${days[1]} + ${days[2]}` 


echo "Toatal is= " $total 


ave=`expr $total \/ 3` 


echo "Average= " $ave 



max=${days[0]} 

min=${days[0]} 



for i in "${days[@]}" 

do 

if [[ "$i" -gt "$max" ]]; then 

    max="$i" 
fi 


if [[ "$i" -lt "$min" ]]; then 

    min="$i" 

fi 

done 


echo "Max is:" $max 

echo "Min is:" $min 


this is killing me lol 
Mon incidents is the highest 11 
Tue incidents is the lowest 2 
------------------------ 

Enter a Day that you'd like to see how many incidents occurred? Wed 
12 Incidents 
------------------------------- 
+0

在正确格式化你的代码题。另外,你是不是也想阅读这些日子?目前您只是阅读活动。 – kabanus

+0

当报告最小/最大值时,3星期一和星期二的事件计数减少了3次? – chepner

+0

对不起,这是我在该网站的第一篇文章。加上我对bash很陌生。 –

回答

0

取代

echo -n "Mon= " read days[0] 

通过

read -p "Mon= " days[0] 

也更换

expr something 

通过

$((somthing)) # space matters 

,并请格式化你的代码,因此它在这个问题可读。

+0

我做了你的建议,没有什么不同! –

0

假设你的天,你写(您可以使用索引,但是你可以用“星期二”“星期三”等,为您阵列):

for i in ${!days[@]}; do 
    if [[ ${days[$i]} -gt "$max" ]]; then 
     maxday="$i" 
    fi 
    if [[ ${days[$i]} -lt "$min" ]]; then 
     minday="$i" 
    fi 
done 

现在你可以打印minday,和天[$ minday ]。如果您将您的密钥作为索引,则需要将索引转换为日期。如果您希望您的阵列很好地格式化:

declare -A days  
echo Tue= 
read days['Tue'] 

等,如果你定义了天。因此,用上述循环,可以打印结果为:

echo $maxday yields the maximum ${days[$maxday]} 
+0

我看不出我怎么能打印结果为“星期一有最高事件14”..我是新手对不起..我可以打印最高和最低值,但我希望它将它链接到日期名称 –

+0

编辑显示。 – kabanus