如何在我的Calculator BASH脚本中修复我的乘法

时间:2017-05-23 11:50:58

标签: bash

我一直在BASH为学校项目制作计算器。但是,乘法代码不起作用。

    #!/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

2 个答案:

答案 0 :(得分:2)

shell会将*展开到当前目录(PWD)中的所有文件,这个过程名为“globbing”,除非转义:

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

祝你好运!完成后,我建议您提交review代码以了解详情。

答案 1 :(得分:2)

不要使用result=$((num1 * num2)) ,而只需使用arithmetic expansion

{{1}}