Question

Build a bash script in Unix to simulate a calculator. It should behave like this: Enter...

Build a bash script in Unix to simulate a calculator.

It should behave like this:

Enter numone: 4

  Enter operator: +

Enter numtwo: 4

4 + 4 = 8

  Enter operator: -

Enter numtwo: 4

8 - 4 = 4

  Enter operator: MS

4 -> M

  Enter operator: *

Enter numtwo: -2

4 * -2 = -8

  Enter operator: C

results cleared

Program ends once "exit" is entered.

It will work like regular calculator which will only ask for first operand once. The result from previous calculation will become the value for first operand.

Homework Answers

Answer #1
# !/bin/bash 
  
# Take user Input 
operator="X"
echo "Enter first number : "
read a 


while true; do  

echo "Enter operator:"
read operator

if [ "$operator" != "exit" ];
then
        echo "Enter second number:"
        read b
fi
case $operator in
  "+")res=`echo $a + $b | bc`
  ;; 
  "-")res=`echo $a - $b | bc` 
  ;; 
  "*")res=`echo $a \* $b | bc` 
  ;; 
  "/")res=`echo "scale=2; $a / $b" | bc` 
  ;; 
  "exit") exit 0 
  ;; 
esac
echo "Result : $res"

a=$res

done
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This will be my third time submitting this question. THERE SHOULD BE NO USE OF CSS...
This will be my third time submitting this question. THERE SHOULD BE NO USE OF CSS OR SWITCH STATEMENTS IN THE JAVASCRIPT. Even though there is stylesheet in the HTML file do no create a new one. Project Standards: Students will use click events to capture user input. Students will use variables to store information needed by their application and keep track of their program’s state. Students will use conditionals to control project flow. Project Task You will be building...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
In narrative essay format, I want you to address a business/organization case study using multiple concepts...
In narrative essay format, I want you to address a business/organization case study using multiple concepts from class. The case question and case text begin on page 5 of this document. You need to demonstrate their best understanding of management and organizational behavior theory, and the application of those ideas to improve the understanding of various issues. You need to clearly identify at least 3 distinct, substantive issues. For each issue you need to 1), identify evidence from the case...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...