1. The problem statement, all variables and given/known data:
Write a shell script that finds and display the sum of even positive integers from 0 to 100. Use the while control structure. Show your script and a sample run.
2. Relevant commands, code, scripts, algorithms:
Must us a while loop
3. The attempts at a solution (include all code and scripts):
This is one attempt:
Another attempt, just trying to get somewhere and have it print something, but with this one I was getting a syntax error..
Portland Community College, Portland, OR, United States, Shawli Sengupta, CS-140U-0-14858 - Intro to UNIX/Linux
I cannot add the URL to the course because I do not have enough post on this forum.
Did you read the answers to your other thread attentively? They mentioned the "step" parameter of the for command - you might try to adapt it to your problem?
And, you might want to read the man page of your shell to learn about the correct syntax of the (mandatory to your problem) while statement - as you used it, the shell will issue a "syntax error".
What shell are you using for your class assignment?
What do your class notes, your text book, or the manual page for your shell say is the proper format for a while loop? Since your assignment says you have to use a while loop, that would seem to be a good place to start (especially since neither of your coding attempts contains a while loop that isn't getting a syntax error).
Note that the syntax for a while loop is not the same as the syntax for a for loop. And, the script that you are writing does not need two loops. If you need to use a while loop, you do not need a for loop to get the job you are trying to do done.
As a hint, I would say that if you're using a while loop to solve this problem, anything using {1..100} is probably not going to help. Using that construct does not fit with the syntax of a while loop. And, after reading your other thread, I would comment that when you said you didn't know how to add numbers that weren't supplied as input from a user, you were wrong. The statement in your previous thread:
did exactly that. Neither sum nor n were provided by a user, but you have a statement that is adding two numbers together when neither of those numbers were typed in by a user. This statement, or one very similar to it, is likely to be needed in your script. It doesn't contain any syntax errors and it does add two numbers together and store the updated results correctly.
These 2 Users Gave Thanks to Don Cragun For This Post:
Instead of incrementing the counter by 1 in the loop and then testing if it even, you could also increment the counter by 2 at every iteration. That way you would not need to test if it is even if the initial value of the counter is even.
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)
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)
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)
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)
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)
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)
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)
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)