Sum of even numbers from 0 to 100 script


Login or Register to Reply

 
Thread Tools Search this Thread
# 8  
Old 1 Week Ago
Hi Nastybutler...

Take a look at my pseudocode, post #5 again.
Your code; there are numerous minor errors and to go through them all before you have solved your project would mean I would be writing it.
Let's get your logic sorted first. You will see from my CODE section how different you logic is from mine.

These lines are fine:
(Just a note, there is no need for the double brackets [[ ]] for this example, single brackets [ ] would suffice. It will still work with double brackets however.)
Code:
#!/bin/bash

counter=0
sum=0

while [[ $counter -le 100 ]]
do
        Important line. "do your arithmetic here which uses both SUM and COUNT"
        Unimportant OPTIONAL print. This can be omitted.
        Important line. "increase your COUNTer by your NEEDED amount"
done
IMPORTANT "print your final value here and should equal the last line of you optional listing"

Again read Don's post #4 thoroughly...

Have another go, and if you increase your "counter" by a certain even number then you will see it all fall into place.
And finally, please use CODE tags, the "</>" icon on the tool bar.
# 9  
Old 1 Week Ago
Hi wisecracker,
Please don't impose your method of performing this task on Nastybutler. Let Nastybutler choose whatever increment he wants. Either of the obvious choices can yield perfectly fine results for this task and as soon as the syntax error is resolved, the logic errors are likely to reveal themselves very quickly. Infinite loops tend to become obvious quickly when there is an echo inside that loop.

As RudiC said in Nastybutler's other thread, if the goal here was to write efficient code, there would be no requirement for a while loop. It appears that the goal of this assignment is to learn how to correctly write a while loop. Whether or not that while loop contains an if statement isn't the point.

With the error that Nastybutler is currently seeing, there are three likely things that could be causing the problem:
  1. A syntax error in the if statement.
  2. A syntax error in the statement before the fi keyword that wasn't fully diagnosed until the fi was seen.
  3. Or, something like a carriage-return in a DOS line terminating <carriage-return><newline> character pair when a single-character <newline> UNIX line terminator was expected on one of the two lines before the fi.
Like you, I'm guessing that the problem is point #2 above, especially since the shell I use complains about an unexpected "(" two lines before the spot that the shell Nastybutler is using complains about something "near the unexpected token `fi'". But, if adding the missing character to line 12 doesn't solve the problem, #3 above becomes a real possibility.
This User Gave Thanks to Don Cragun For This Post:
Neo (1 Week Ago)
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Sum even numbers from 1 to 100 Nastybutler UNIX for Beginners Questions & Answers 5 2 Weeks Ago 03:35 AM
Sum of numbers in row smitty11 Shell Programming and Scripting 13 12-20-2016 09:18 AM
Shell script count lines and sum numbers from multiple files Elly Shell Programming and Scripting 5 11-18-2016 02:56 AM
Script Shell: Count The sum of numbers in a file chercheur111 Shell Programming and Scripting 3 05-23-2016 01:02 PM
Sum up numbers in a for loop IMPe Shell Programming and Scripting 7 02-06-2014 06:17 PM
Sum of numbers in three or more files Natalie Shell Programming and Scripting 12 10-10-2013 11:20 AM
Sum Numbers from different files saint2006 Shell Programming and Scripting 5 04-02-2011 06:07 AM
Sum of decimal numbers in column tdev457 Shell Programming and Scripting 4 09-20-2010 08:29 PM
getting the sum of numbers zerofire123 Shell Programming and Scripting 2 06-10-2010 09:06 PM
Finding the sum of two numbers prasperl Shell Programming and Scripting 3 05-29-2010 08:07 AM
Help with shell script to find sum of first n numbers of Fibonacci series Kshitija Homework & Coursework Questions 0 03-28-2010 08:22 AM
Shell script to find the sum of first n Fibonacci numbers Kshitija Shell Programming and Scripting 1 03-28-2010 07:49 AM
Printing the sum of a numbers chrs0302 Shell Programming and Scripting 4 06-26-2009 11:41 AM
sum numbers from stdout umen Shell Programming and Scripting 1 08-05-2006 07:26 AM
how to sum numbers in column iahveh Shell Programming and Scripting 1 09-30-2004 07:50 AM