Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 10th, 2004, 1:45 PM   #1
Pimp Daddy
Programmer
 
Join Date: Aug 2004
Location: Wherever the beautiful women are.
Posts: 30
Rep Power: 0 Pimp Daddy is on a distinguished road
Send a message via MSN to Pimp Daddy Send a message via Skype™ to Pimp Daddy
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)
Pimp Daddy is offline   Reply With Quote
Old Oct 12th, 2004, 3:56 AM   #2
elmo
Newbie
 
Join Date: Oct 2004
Posts: 4
Rep Power: 0 elmo is on a distinguished road
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
elmo is offline   Reply With Quote
Old Oct 12th, 2004, 8:39 AM   #3
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
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!
Pizentios is offline   Reply With Quote
Old Oct 12th, 2004, 2:01 PM   #4
Pimp Daddy
Programmer
 
Join Date: Aug 2004
Location: Wherever the beautiful women are.
Posts: 30
Rep Power: 0 Pimp Daddy is on a distinguished road
Send a message via MSN to Pimp Daddy Send a message via Skype™ to Pimp Daddy
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.
__________________
David Morris BSc.(Hons), MBCS
Qualified Computer Engineer
Administrator (SEED Software)
Pimp Daddy is offline   Reply With Quote
Old Oct 12th, 2004, 3:14 PM   #5
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
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!
Pizentios is offline   Reply With Quote
Old Oct 14th, 2004, 12:54 AM   #6
erebus
Programmer
 
erebus's Avatar
 
Join Date: Aug 2004
Location: /dev/null
Posts: 65
Rep Power: 4 erebus is on a distinguished road
Send a message via AIM to erebus
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'>&quot;Political power grows out of the barrel of a gun&quot; - Mao Tse-Tung</span></span>
erebus is offline   Reply With Quote
Old Oct 14th, 2004, 4:19 AM   #7
Pimp Daddy
Programmer
 
Join Date: Aug 2004
Location: Wherever the beautiful women are.
Posts: 30
Rep Power: 0 Pimp Daddy is on a distinguished road
Send a message via MSN to Pimp Daddy Send a message via Skype™ to Pimp Daddy
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)
Pimp Daddy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:10 AM.

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