![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
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?
__________________
David Morris BSc.(Hons), MBCS Qualified Computer Engineer Administrator (SEED Software) |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Oct 2004
Posts: 4
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() |
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.
__________________
Profanity is the one language that all programmers understand. Check out my Blog <---updated Nov 30 2007! |
|
|
|
|
|
#4 | |
|
Programmer
|
Thanks for the help. Seems to make sense.
Quote:
__________________
David Morris BSc.(Hons), MBCS Qualified Computer Engineer Administrator (SEED Software) |
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() ![]() |
ok, cool, just didn't want you re-inventing the wheel for nothing.
__________________
Profanity is the one language that all programmers understand. Check out my Blog <---updated Nov 30 2007! |
|
|
|
|
|
#6 |
|
Programmer
|
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
__________________
<span style='font-size:14pt;line-height:100%'><span style='color:red'>"Political power grows out of the barrel of a gun" - Mao Tse-Tung</span></span> |
|
|
|
|
|
#7 |
|
Programmer
|
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.
__________________
David Morris BSc.(Hons), MBCS Qualified Computer Engineer Administrator (SEED Software) |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|