Sum of even numbers from 0 to 100 script


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Sum of even numbers from 0 to 100 script
# 8  
Old 03-05-2019
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 03-05-2019
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:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sum even numbers from 1 to 100

I need help with this assignment. I'm very new to using UNIX/LINUX, and my only previous experience with programing anything is using python. We are writing scripts using vim, and this one I'm stumped on. "Write a shell script that finds and display the sum of even positive integers from 0 to... (5 Replies)
Discussion started by: Nastybutler
5 Replies

2. Shell Programming and Scripting

Shell script count lines and sum numbers from multiple files

I want to count the number of lines, I need this result be a number, and sum the last numeric column, I had done to make this one at time, but I need to make this for a crontab, so, it has to be an script, here is my lines: It counts the number of lines: egrep -i String file_name_201611* |... (5 Replies)
Discussion started by: Elly
5 Replies

3. Shell Programming and Scripting

Script Shell: Count The sum of numbers in a file

Hi all; Here is my file: V1.3=4 V1.4=5 V1.1=3 V1.2=6 V1.3=6 Please, can you help me to write a script shell that counts the sum of values in my file (4+5+3+6+6) ? Thank you so much for help. Kind regards. (3 Replies)
Discussion started by: chercheur111
3 Replies

4. Shell Programming and Scripting

Sum Numbers from different files

Hi All, I need to print the sum of numbers from different files. Input files: file1.out 10 20 30 file2.out 10 20 30 (5 Replies)
Discussion started by: saint2006
5 Replies

5. Shell Programming and Scripting

getting the sum of numbers

I basically have a file where I had to do a bunch of greps to get a list of numbers example: a file called numbers.txt 10000 10000 superman 10000 batman 10000 10000 grep '100' * | 10000 10000 10000 10000 10000 (2 Replies)
Discussion started by: zerofire123
2 Replies

6. Shell Programming and Scripting

Finding the sum of two numbers

cat *.out |grep "<some text>" | awk '{print $6}' For ex,This will reutrn me 11111 22222 is it possible to add these two numbers in the above given command itself?I can write this to a file and find the sum. But I prefer to this calculation in the above given line itself. Any... (3 Replies)
Discussion started by: prasperl
3 Replies

7. Homework & Coursework Questions

Help with shell script to find sum of first n numbers of Fibonacci series

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Shell script to find sum of first n numbers of Fibonacci series 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: Kshitija
0 Replies

8. Shell Programming and Scripting

Shell script to find the sum of first n Fibonacci numbers

pls give me the solution for this i need it for my exam pls pls pls Shell script to find the sum of first n Fibonacci numbers (1 Reply)
Discussion started by: Kshitija
1 Replies

9. Shell Programming and Scripting

sum numbers from stdout

hello im looking for short way to sum numbers from stdout the way i found to do it is to long for me i wander if there is shorter way to do it ok it 2 stage action this will make the list of number in to file sum.txt grep -c include *.c | awk '{l=split($0,a,":");print a;}' > sum.txt this... (1 Reply)
Discussion started by: umen
1 Replies

10. Shell Programming and Scripting

how to sum numbers in column

Hi, i want to sum all nubers in one column. Example: 12.23 11 23.01 3544.01 I'm trying to do this in awk, but it doesn't work properly. Seems like awk is summing only integers, for example: 12 11 23 3544 It cuts off numbers after dot. I used this command: akw /text/ file.txt |nawk... (1 Reply)
Discussion started by: iahveh
1 Replies
Login or Register to Ask a Question