LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-07-2012, 01:11 AM   #1
kross2100
LQ Newbie
 
Registered: Dec 2012
Posts: 9

Rep: Reputation: Disabled
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!
 
Old 12-07-2012, 01:25 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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/
 
1 members found this post helpful.
Old 12-07-2012, 01:41 AM   #3
kross2100
LQ Newbie
 
Registered: Dec 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
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

Last edited by kross2100; 12-07-2012 at 01:45 AM.
 
Old 12-07-2012, 04:57 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
If you add my debug suggestion, you'll see the problem/solution.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk syntax for calculating average and standard deviation vjramana Programming 6 04-21-2011 10:36 AM
Calculating average from Python Script stryker213 Programming 5 04-25-2010 05:58 PM
Reading a .CSV file and then calculating average per minute basis in shell script. krishdeeps Linux - Newbie 1 04-23-2010 05:38 PM
Calculating Average in PHP Array? your_shadow03 Programming 1 01-27-2010 05:19 AM
calculating average of a column of numbers in bash hta1984 Programming 4 12-13-2006 12:58 PM


All times are GMT -5. The time now is 04:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration