Link to home
Start Free TrialLog in
Avatar of djwburr
djwburr

asked on

Shell script that implements a simple calculator for integer arithmetic.

Hey Guys/Gals... I'm interested in writing a shell script that implements a simple calculator for integer arithmetic. The calculator should prompt the user and receive its input in following manner:

Enter the first operand: 4
Enter an operator: *
Enter the second operand: 3
Answer: 4 * 3 = 12
Assume that in this example the user entered 4, * and 3 in response to the scripts prompting.
The permissible operators that the calculator should be able to handle are: +,-,*,/ and %. The
calculator should continue running until the user enters the character x at any of the prompts.

Thanks in advance for your help!
Avatar of skyper3
skyper3

Hm, is there a class which has homework in shellscripting? ;)
You'd have to use a combination of expr, read and test
echo '4*3'|bc
echo '3 4 * p'|dc

This is too stupid for a real homework :-))
Avatar of djwburr

ASKER

I'm trying out some exercises I came across in a book and having problems with a few. The calculator is supposed to be able to accept other integers and not just the ones in the example. Thanks again
ASKER CERTIFIED SOLUTION
Avatar of skyper3
skyper3

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ROFLOL, 10 lines of code what could be written in one line with just 9 characters (beside the payload), I guess it could be improved to a modern application with some gigs of code and data and bugs and ... ;-))

(skyper3, no offence at all, just about what was asked for and accepted)

BTW, RES=$(....)  is invalid syntax for sh (read the manuals:)