LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Syntax Error when calculating an average (https://www.linuxquestions.org/questions/linux-newbie-8/syntax-error-when-calculating-an-average-4175440378/)

kross2100 12-07-2012 12:11 AM

Syntax Error when calculating an average
 
SO here is a question on my homework:
Write a program to continue taking numbers from the user until he/she enters a null value. When so, the program should show the average value.

So here is the code I used
Code:

1 echo "Keep entering numbers until you want to stop"
  2 read num
  3 total=0
  4 count=0
  5 while [ -n "$num" ]
  6 do
  7 read num
  8 let total=$total+$num
  9 let count=$count+1
 10 done
 11 let average=$total%$count
 12 echo "The average of all those numbers is "$average

I keep getting a Syntax error on line 8 for some reason... I know its probably a silly newb mistake, but what am I doing wrong here??? Thank you!

chrism01 12-07-2012 12:25 AM

You can use 'set -xv' at the top of your script to debug thus
Code:

#!/bin/bash
set -xv

<your code here>

Anyway, as per this page, http://www.tldp.org/LDP/abs/html/internal.html#LETREF, you don't need the '$' sign.

Next time show the exact error msg you get.

Useful links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

kross2100 12-07-2012 12:41 AM

my error message was as such:

Code:

./average: line 8: let: total=20+: syntax error: operand expected (error token is "+")
this was after passing the values 1, 2, 3, 4, 5, and 6

chrism01 12-07-2012 03:57 AM

If you add my debug suggestion, you'll see the problem/solution.


All times are GMT -5. The time now is 06:33 PM.