simple shell script....complex migraine

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2005
Posts: 4
Reputation: nobius is an unknown quantity at this point 
Solved Threads: 0
nobius nobius is offline Offline
Newbie Poster

simple shell script....complex migraine

 
0
  #1
Mar 20th, 2005
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:
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 4
Reputation: nobius is an unknown quantity at this point 
Solved Threads: 0
nobius nobius is offline Offline
Newbie Poster

Re: simple shell script....complex migraine

 
0
  #2
Mar 21st, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 4
Reputation: nobius is an unknown quantity at this point 
Solved Threads: 0
nobius nobius is offline Offline
Newbie Poster

Re: simple shell script....complex migraine

 
0
  #3
Mar 21st, 2005
[HTML]<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>

[/HTML]
That is what i have so far...any suggestions would be nice thank you
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum


Views: 9138 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC