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:

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

<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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.