1. Registered User
Join Date
Oct 2006
Posts
6

hi everyone i need help with this shell script:

Write a script that gives you the total of all even numbers. For example, if a user enters 5 then the program should give you 6, because 1, 2, 3, 4, 5 an The addition of all even numbers is 2 + 4 = 6.

so the script has to be a while loop which will add only even numbers. if i put 10 the script has to only add 2+4+6+8+10 to give me the total

thank you

2. Registered User
Join Date
Jun 2003
Location
West Palm Beach, FL
Posts
2,713

Homework?

Try coding something and maybe we will help.

3. Registered User
Join Date
Oct 2006
Posts
6
well, this prof is really messing us up. we just started this course no practise. i dont have any code.

4. Registered User
Join Date
Jun 2003
Location
West Palm Beach, FL
Posts
2,713
Originally Posted by nyst8fmind
well, this prof is really messing us up. we just started this course no practise. i dont have any code.
There are several ways to code this script, which commands have you learned?

5. Registered User
Join Date
Oct 2006
Posts
6
the while loop, if ,the, elif,else

6. Registered User
Join Date
Aug 2006
Location
The Netherlands
Posts
248
Dirty trick...
Code:
```#!/bin/bash

if [ \$# -ne 1 ]; then
exit 1
elif [ `echo \$1 | egrep '[^0-9]'` ]; then
echo "Please supply a real number..."
exit 1
fi

echo \$1 | awk ' {
for(i = 1; i <= \$0; i++)
if(i % 2 == 0)
sum += i
print sum
}'```
and is probably not what your teacher had in mind. But this is the way I would do it because awk is much easier with loops and arithmetic expressions. And this is the long version...

Regards

7. Registered User
Join Date
Oct 2006
Posts
6

## shell script

so if i excute this and i input any number its gonna add all the even numbers, regardless of what number i would input:
example when i do ./"file name"
its gonna say enter number: so i put lets say 21 hit enter, then it will add all the even number between 1 and 21? and give me the total.
i'm really sorry if i'm being too much, i wish i could explain my circumstances.

thank you very much for your help

8. Registered User
Join Date
Aug 2006
Location
The Netherlands
Posts
248
Code:
```#!/bin/bash

if [ \$# -lt 1 ]; then
exit 1
elif [ `echo \$1 | egrep '[^0-9]'` ]; then
echo "Please supply a real number..."
exit 1
fi

i=1
j=0
while [ \$i -le \$1 ]; do
if [ `expr \$i % 2` -eq 0 ]; then
let j=\$j+\$i
fi
let i=\$i+1
done
echo \$j```
Here's the pure shellscript version of the thing. Some things can be done in shorthand but this is written out completely for understanding.
First there's a check for the existence of at least one argument and second there's a check for the type of the first argument; if it contains characters other than figures ([^0-9]) it bails out. Any possible other argument is ignored.
Figure out the calculation part yourself, I only can say that the expression `expr \$i % 2` performs a modulo calculation to check whether te value of the counter \$i is odd or even.

Good luck

9. Registered User
Join Date
May 2005
Location
South Africa
Posts
1,369
Alternate code to play with
Code:
```#!/usr/bin/bash
while :
do
printf "Enter integer: "
case \$int in
*[^0-9]*) printf "Error. " ;;
*) break;;
esac
done
int=\$((\$int-\$int%2))
sum=0
while [ \$int -gt 1 ]
do
sum=\$((\$sum+\$int))
int=\$((\$int-2))
done
printf "\$sum\n"```

10. Registered User
Join Date
May 2005
Location
South Africa
Posts
1,369
Private Message
Originally Posted by nyst8fmind
...can you explain to me step by step the functions...

man bash
Code:
```while list; do list; done
The while command continuously executes the do list  as
long as the last command in list returns an exit status
of zero.

: [arguments]
No effect; the command does  nothing  beyond  expanding
arguments and performing any specified redirections.  A
zero exit code is returned.

printf format [arguments]
Write the formatted arguments to  the  standard  output
under the control of the format

read a line from standard input

case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
A  case  command first expands word, and tries to match
it against each pattern in turn, using the same  match-
ing  rules  as  for  pathname  expansion  (see Pathname
Expansion  below).   When  a  match   is   found,   the
corresponding list is executed.  After the first match,
no subsequent matches are attempted.  The  exit  status
is  zero  if  no pattern matches.  Otherwise, it is the
exit status of the last command executed in list.

[ expr ]
Return a status of 0 or 1 depending on  the  evaluation
of  the conditional expression expr.

name=value
the value of the variable name is set to value.

\$((expression))
Arithmetic expansion allows the evaluation of an arithmetic
expression and the substitution of the result.```

11. Registered User
Join Date
Oct 2006
Location
amara no meikyuu
Posts
15
or just use grade school binomial theorem to derive the formula that will allow you to evaluate the function in O(c) instead of O(n)

i remember an anecdote about a scientist, bohr i think, who answered the question "Describe how to determine the height of a skyscraper with a barometer." with ... well thats left as homework he went on to garner the nobel prize in 1922 for his work on atomic structure and the hydrogen atom. he was 37. mmm reminds me of how old i am getting. i have to win one nobel prize soon ... but im too dumb to
Last edited by payawaljohn; 10-29-06 at 14:36.

12. Registered User
Join Date
May 2005
Location
South Africa
Posts
1,369