LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-18-2006, 07:24 AM   #1
kb100
Member
 
Registered: Aug 2006
Posts: 43

Rep: Reputation: 15
simple scripting help please


Hi there again. Wonder if anyone could help. How can you prompt a user if they would like to perform either, add, subtract, multiply or divide when imputting two numbers?

For example i ask a user for a number, then i ask the user if they would like to use an add, multiply, subtract or divide operation to calculate a second integer.

thanks

Last edited by kb100; 08-18-2006 at 07:33 AM.
 
Old 08-18-2006, 07:33 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
There are many ways, what have you in mind ?
 
Old 08-18-2006, 07:36 AM   #3
kb100
Member
 
Registered: Aug 2006
Posts: 43

Original Poster
Rep: Reputation: 15
Sorry im trying to edit the message but my internet keeps stopping. What i want to do is ask the user to input an integer then i ask the user if they would like to use either an add, subtract, multiply or divide function. then i ask the user for a second integer and the result will appear.

thanks again
 
Old 08-18-2006, 07:38 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
That looks just fine, which parts of this scenario have you trouble with ?
 
Old 08-18-2006, 07:41 AM   #5
kb100
Member
 
Registered: Aug 2006
Posts: 43

Original Poster
Rep: Reputation: 15
i do not know how or what to use for the mathematics calculations.
 
Old 08-18-2006, 07:43 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
The most portable is expr, eg:
Code:
expr $a + $b
will print the sum of a and b variables.
 
Old 08-18-2006, 07:55 AM   #7
kb100
Member
 
Registered: Aug 2006
Posts: 43

Original Poster
Rep: Reputation: 15
How would i be able to give the choice to the user of what calculation he/she would like?

shall i give the user a keyboard choice, eg, if user presses the letter a the operation would add?

what will be the best way of doing this?

thanks for your reply.
how does everyone know how to do these things here??!!
 
Old 08-18-2006, 07:57 AM   #8
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Rep: Reputation: 15
Well...probably they Googled for a bash tutorial
 
Old 08-18-2006, 07:58 AM   #9
kb100
Member
 
Registered: Aug 2006
Posts: 43

Original Poster
Rep: Reputation: 15
alright mate
 
Old 08-18-2006, 08:19 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by kb100
How would i be able to give the choice to the user of what calculation he/she would like?

shall i give the user a keyboard choice, eg, if user presses the letter a the operation would add?
That seem sensible.

Quote:
what will be the best way of doing this?
There is no "best way", and even if there is one, you should first focus on one that works.
Quote:
how does everyone know how to do these things here??!!
BeThian got it right, at least for the youngers of us.
 
Old 08-18-2006, 08:22 AM   #11
kb100
Member
 
Registered: Aug 2006
Posts: 43

Original Poster
Rep: Reputation: 15
Hi there, figured it out and it all works. Thanks. but what symbol do you use for multiply? *?

thanks
 
Old 08-18-2006, 08:31 AM   #12
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
How would i be able to give the choice to the user of what calculation he/she would like?
You could use "select" to present choices or just use a plain "case".


how does everyone know how to do these things here??!!
Find neat script or have problem. Look at other people's code.
How do they do that? How am I gonna fix this? Write own code.
Reinstall Linux ;-p and find out hard way need reading:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html then
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html finally
http://www.tldp.org/LDP/abs/html/
Write own code. Reinstallation postponed by using "echo rm" instead of "rm" ;-p
Write some more. Typo scorches patch in side of computer casing.
Find out hard way need more examples. Search:
search LQ Bookmarks for bash and tutorial or guide
search LQ for "bash tutorial"
also see http://www.linuxquestions.org/questi...threadid=27957
and finally you can ask discuss things and ask questions ;-p

Difference is how much you want to leech vs learn.
 
Old 08-18-2006, 08:34 AM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Yes, multiply is a problem.

The operator is the expected * , but the shell expands it before expr has a chance to get it, so you need to escape it that way:
Code:
expr 2 \* 5
or if you use variables for the whole thing:
Code:
first=2
second=5
operand=*
eval expr $first \\$operand $second
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
simple scripting help please kb100 Programming 3 08-18-2006 08:08 AM
simple shell scripting problem noir911 Programming 11 03-14-2006 01:27 AM
Very simple BASH scripting question clickster Linux - Newbie 6 11-23-2005 04:28 PM
simple scripting question wedgeworth Programming 6 02-04-2004 10:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration