5

Need help with Linux Bash script. Essentially, when run the script asks for three sets of numbers from the user and then calculates the numbers inputted and finds the average.

#!/bin/bash
echo "Enter a number: "
read a
   while [ "$a" = $ ]; do

echo "Enter a second set of numbers: "
read b
b=$
   if [ b=$ ]

Am I going about this wrong?

Floris
  • 45,857
  • 6
  • 70
  • 122
user3089320
  • 53
  • 1
  • 1
  • 3
  • Why do you ask? Is it not working? What inputs are you using, and what are your results? – Floris Dec 11 '13 at 02:41
  • I'm unsure of where to go from here. The script should ask for the users number but after that I'm not sure how to calculate for the average. – user3089320 Dec 11 '13 at 02:47
  • What is the goal of the first number you read? Is it just the first in a list of numbers? Do you expect the user to enter carriage returns between numbers? Or does CR signify end of input? Please clarify how you want the input so we can begin to help. – Floris Dec 11 '13 at 02:53
  • I want the script to ask the user to Enter any 3 sets numbers a, b, c and using if logic, calculates the average of those 3 sets of numbers ([a+b+c / 3]) and displays the answer. That's all. Forgive me for I'm learning Unix and the book is of no use. – user3089320 Dec 11 '13 at 03:04
  • Do you want three numbers, or three "sets" of numbers? – Floris Dec 11 '13 at 03:19

4 Answers4

5

Still not sure what you want a to be. But I think you can just loop 3 times. Then each iteration get a set of numbers, and add them up and keep track of how many you have. So something like below. (note $numbers and $sum are initialized to 0 automatically)

#!/bin/bash    
sum=0
numbers=0
for a in {1..3}; do
  read -p $'Enter a set of numbers:\n' b
  for j in $b; do
    [[ $j =~ ^[0-9]+$ ]] || { echo "$j is not a number" >&2 && exit 1; } 
    ((numbers+=1)) && ((sum+=j))
  done
done

((numbers==0)) && avg=0 || avg=$(echo "$sum / $numbers" | bc -l)
echo "Sum of inputs = $sum"
echo "Number of inputs = $numbers"
printf "Average input = %.2f\n" $avg                               

Where example output would be

Enter a set of numbers: 
1 2 3
Enter a set of numbers: 
1 2 3
Enter a set of numbers: 
7
Sum of inputs = 19
Number of inputs = 7
Average input = 2.71
Reinstate Monica Please
  • 11,123
  • 3
  • 27
  • 48
  • If OP did indeed want "three sets" then your code is very nice. If it's "a set of three numbers" then it's overkill. Either way you get my vote. – Floris Dec 11 '13 at 03:20
  • Thank you BroSlow for your help. This is exactly what I needed. Works great and easy to use, thanks! – user3089320 Dec 11 '13 at 03:33
  • @user3089320 you should click the little check mark next to this answer to "accept" it, since this is the one you prefer. – Floris Dec 11 '13 at 03:42
  • @MikeQ Don't think the question warranted scripting out every possible erroneous user input, but added that and a numeric input check. – Reinstate Monica Please Jun 17 '14 at 22:31
1

If I understood you correctly, the following code will do what you asked:

#!/bin/bash
echo "Enter three numbers:"
read a b c
sum=$(($a + $b + $c))
count=3
result=$(echo "scale=2; 1.0 * $sum / $count" | bc -l)
echo "The mean of these " $count " values is " $result

Note - I left count as a separate variable so you can easily extend this code.

The use of bc allows floating point arithmetic (not built in to bash); scale=2 means "two significant figures".

Sample run:

Enter three numbers:
3 4 5
The mean of these  3  values is  4.00
Floris
  • 45,857
  • 6
  • 70
  • 122
  • 1
    Floris thank you for your continued support in trying to help me, much appreciated however BroSlow better understood what it was I asked for - no hard feelings I hope! – user3089320 Dec 11 '13 at 03:32
  • None at all - you saw I upvoted his answer. I just couldn't figure out what you were saying - he obvious got it. – Floris Dec 11 '13 at 03:41
0

Test values:

    sum=200232320
    total=300123123

Basic take average and get percentage:

    avg=$(echo "$sum / $total" | bc -l)
    avg=$(echo "$avg*100" | bc -l)
    printf "Average input = %.2f\n" $avg

Basic take average and get percentage with fault tolerance:

    # -- what if sum=0 or greater than total?
    if [ $sum -eq 0 ] || [ $sum -gt $total ] ;then
        avg=0
    else
        avg=$(echo "$sum / $total" | bc -l)
        avg=$(echo "$avg*100" | bc -l)
    fi

    printf "Average input = %.2f\n" $avg

Basic take average and get percentage:

    result=$(echo "scale=6; 1.0 * $sum / $total*100" | bc -l)
    printf "Average input = %.2f\n" $result

Basic take average and get percentage:

    result=$(echo "scale=6; 1.0 * $sum / $total*100" | bc -l) 
    printf "Average input = %.2f\n" $result

Basic take average and get percentage (with tolerance:

    # -- if sum is greater than total we have a problem add in 1.0 to address div by zero
    [[ $sum -gt $total ]] && result=0 || result=$(echo "scale=6; 1.0 * $sum / $total*100" | bc -l) 
    printf "Average input = %.2f\n" $result

output:

    ./test.sh 
    Average input = 66.72
    Average input = 66.72
    Average input = 66.72
    Average input = 66.72
Mike Q
  • 6,716
  • 5
  • 55
  • 62
0
 #!/bin/bash

echo "Enter size"

read s  `#reading size of the average`

i=1          `#initializing` 

sum=0        `#initializing`

echo "Enter the factors"

while [ $i -le $s ]

do
  read factors

sum=$((sum + factors))

i=$((i + 1))

done

avg=$(echo $sum / $s | bc -l)


   echo "Average of factors is" $avg
tech bie
  • 1
  • 1