0

trying to write a shell script that will take any number of numerical parameters passed to it on the command line and perform addition on them...must be done in a "while" loop...have it so that it will add 2 numbers, also have it where it will print the correct sum for 1 or 0 parameters just cant get it to read more than two without hard coding the parameters (i.e. $1 + $2 + $3 . . . ) i think my prob is i am trying to attack it as an array but i cant seem to think of any other way to do it....going mad...slowly going mad.......help me please........... :eek:

1
Contributor
2
Replies
4
Views
12 Years
Discussion Span
Last Post by nobius
0

and yes...this is a hw assignment...but i have been stuck on it for 2 days now and the prof is out of town...cant find any books or help anywhere

0
<code>

#!/bin/bash

param=$#
echo $param
if [ $param -eq 0 ]
then
echo "The sum is 0"
fi
if [ $param -eq 1 ]
then
echo "The sum is $1"
fi
if [ $param -ge 2 ]
then
count=1
while [ $count -le $param ]
do
echo "first param is $[$count]"
echo "second param is $[$count + 1]"
sum=$(($[ $count ] + $[ $count + 1]))
count=$(($count + 1))
done
echo "The sum is $sum"
fi
</code>

That is what i have so far...any suggestions would be nice thank you

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.