BruntXL
BruntXL

Reputation: 1

How to fix my multiplication in my Calculator BASH script

I've been making a calculator in BASH for a school project. However, the multiplication code isn't working.

    #!/bin/bash
        input="yes"
       while [[ $input = "yes" ]]
       do

  #type in the number that correponds to math operation you want to do

     PS3="Press 1 for Addition, 2 for subtraction, 3 for multiplication
    4 for division: "
    select math in Addition Subtraction Multiplication Division
    do
    case "$math" in
    Addition)
    #enter numbers
      echo "Enter first no:"
        read num1
        echo "Enter second no:"
        read num2
        result=`expr $num1 + $num2`
        echo Answer: $result
        break
    ;;
   #enter numbers
    Subtraction)
        echo "Enter first no:"
        read num1
        echo "Enter second no:"
        read num2
        result=`expr $num1 - $num2`
        echo Answer: $result
        break
    ;;
    #thing that needs to be fixed
    Multiplication)
        echo "Enter first no:"
        read num1
        echo "Enter second no:"
        read num2
        $result= expr $num1 * $num2
        echo Answer: $result
        break
    ;;
    #enter numbers
    Division)
        echo "Enter first no:"
        read num1
        echo "Enter second no:"
        read num2
        result=$(expr "scale=2; $num1/$num2" | bc)
        echo Answer = $result
        break
        ;;
        *)
        break
        ;; 
         esac
         done
         done

Upvotes: 0

Views: 149

Answers (2)

Eric Renouf
Eric Renouf

Reputation: 14500

Instead of using expr just use the arithmetic expansion

result=$((num1 * num2))

Upvotes: 2

l0b0
l0b0

Reputation: 58798

The shell expands * to all the files in the current directory (PWD), a process called "globbing", unless escaped:

$ expr 2 * 2
expr: syntax error
$ expr 2 \* 2
4

Good luck with the assignment! Once you're done I would recommend submitting your code for review to learn more.

Upvotes: 2

Related Questions