1

I am trying to write a short script that accepts a variable number of parameters (also numbers) then adds those parameters together to get a total of the numbers. Then gets an average for those numbers entered. This is what i have so far;

#!/bin/bash

count=1
ncount=1
echo
echo "please enter number of parameters: "
read parano

while [ $parano -ge $numbers$count ]
    do
    echo
    echo "Please enter parameter $count: "
    read number$ncount
    let count=count+1
    let ncount=ncount+1

done 

Total=$((number$ncounttotal))
Average=$((Total/parano))

echo
echo "You have chosen $parano parameters"
echo 
echo "The average is $Average"
echo

Its just the line for calculating the total that I am having issues with and cannot seem to find the code to calculate it. The rest seems to be working great but the average always comes out as 0 because of the total not being calculated. Anyone have any ideas?

3
  • You are trying to generate variable names dynamically with $numbers$count and number$counttotal, but that doesn't work. You should probably use an array instead, see here for a detailed discussion. Commented Oct 23, 2016 at 0:04
  • I appreciate the feedback and the link, iv been looking through it but I'm rather new to programming so everything is almost literally going over my head. Could you possibly throw and example at me? @BenjaminW. Commented Oct 23, 2016 at 11:31
  • Turns out I was wrong, it's possible to assign to variables like this with read, but in general you can't generate a variable name dynamically in Bash and you need indirection - but as you found out, arrays are the way to go here. Commented Oct 23, 2016 at 19:50

2 Answers 2

1
#!/bin/bash

[ $# -eq 0 ] && exit 1

for number in $@; do
    sum=$(($sum + $number))
done

average=$(echo "$sum / $#" | bc -l)

echo $average

Then call it like:

./shellscript 1 2 3 

I use bc above since bash will only do integer arithmetic and that's not great for the average.

Sign up to request clarification or add additional context in comments.

Comments

0

Thank you for the help guys. The answer to what I needed was an array as mentioned.

#!/bin/bash

echo
echo "Please enter number of parameters: "
read parano

count=1
Total=$((0))

while [ $parano -ge $numbers$count ]
    do
    echo
    echo "Please enter parameter $count: "
    read number
    let count=count+1
    Total=$(($Total+number))

done 

Average=$((Total/parano))

echo
echo "You have chosen $parano parameters"
echo 
echo "The total is $Total "
echo
echo "The average is $Average"
echo

Answer was to make an array as the total and keep adding the variables to the array as they went along. At least that's what I think it is. Either way it works so thanks all!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.