Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Bash / Shell Scripting (http://www.programmingforums.org/forum26.html)
-   -   Calculator Problem (http://www.programmingforums.org/showthread.php?t=797)

Pimp Daddy Oct 10th, 2004 1:45 PM

I'm creating a script that acts as a basic calculator. This will eventually be improved so it contains several advanced functions. Here is the problem that I have at the moment:

:

function Addition {
clear
echo "How many numbers? "
read numbers
echo "Please enter the first number: "
read total
for i in `seq 1 $numbers`;
        do
    echo "Plese enter the next number: "
  read adding
  total=$total+$adding
        done
echo ""
echo ""
echo "The answer is: "
echo $total
Menu
}


When I enter 3 as the amount of numbers in the calculation, the loop always occurs four times and after that skips the next few lines and goes back to the Menu function, not providing me with the answer. Any ideas?

elmo Oct 12th, 2004 3:56 AM

the script doesnt loop 4 times, but you ask for a number before it starts.
example:
3 numbers
insert one number: 1

then the loop starts to run 3 times, resulting in a total of 4 numbers

the way around this is to do something like this:

:

...
for i in `seq 1 $(($numbers - 1))`;
do
...


should give you the desired result

Pizentios Oct 12th, 2004 8:39 AM

If your creating a script for linux there's one ether already installed and or availble for download on the net. I know you probally just doing this to learn scripting, but i'd thought that i would tell you that just in case.

Pimp Daddy Oct 12th, 2004 2:01 PM

Thanks for the help. Seems to make sense.


Quote:

If your creating a script for linux there's one ether already installed and or availble for download on the net. I know you probally just doing this to learn scripting, but i'd thought that i would tell you that just in case.
Yes, I am just doing it to learn more about shell scripting. I don't intend to use it regularly and should be making much more advanced things in the future.

Pizentios Oct 12th, 2004 3:14 PM

ok, cool, just didn't want you re-inventing the wheel for nothing.

erebus Oct 14th, 2004 12:54 AM

I didn't look at the above code, since i'm kinda on the go right now, but doing simple calculations on the commandline can be a lot less tedious. Maybe you could build something bigger from this:

:

$ echo '5+2*10/2' | bc

Pimp Daddy Oct 14th, 2004 4:19 AM

Thanks for that - it does look easier. I'll try and work that into a future version. At the moment though I'm working on another Bash script where I enter the name of a particular folder, creates a tar file as a backup, and puts that tar file on my external hard disk. It'll save me from having to type out every script and re-rip every audio file if I have to reinstall Linux again.


All times are GMT -5. The time now is 3:33 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC