| | |
simple shell script....complex migraine
Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2005
Posts: 4
Reputation:
Solved Threads: 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:
•
•
Join Date: Mar 2005
Posts: 4
Reputation:
Solved Threads: 0
[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
#!/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
![]() |
Similar Threads
- Simple shell script (Shell Scripting)
- Call a shell script from a python script (Python)
- Simple Shell Script Question.... [java related] (Shell Scripting)
- shell script help (Shell Scripting)
- Need help in Shell script (Shell Scripting)
- A simple perl script (Perl)
- Problem with variables in Windows shell script (Windows NT / 2000 / XP)
Other Threads in the Shell Scripting Forum
- Previous Thread: Learn TCL in 21 days or less
- Next Thread: Time control with Shell script
Views: 9138 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Shell Scripting





