linux 在bash case语句中作为模式的特殊符号

yptwkmov  于 2022-11-02  发布在  Linux
关注(0)|答案(1)|浏览(112)

我正在做一个简单的计算器。用户输入简单的表达式,如1+2或11*4或24/2。我需要使用case结构来回显计算,如“3乘4等于12”。运行程序时,Atm case结构不回显?


# !/bin/bash

read -p "Give expression example: 42+6 " calc
read NUM1 NUM2 <<<${calc//[^0-9]/ }

case $calc in
    \+)
    echo "$NUM1 plus $NUM2 equals $NUM1+$NUM2"
    ;;
    \-)
    echo "$NUM1 minus $NUM2 equals $NUM1-$NUM2"
    ;;
    \*)
    echo "$NUM1 multiplied by $NUM2 equals $NUM1*$NUM2"
    ;;
    \/)
    echo "$NUM1 divided by $NUM2 equals $NUM1/$NUM2"
    ;;
esac
2ic8powd

2ic8powd1#

$calc是输入的原始字符串


# !/bin/bash

read -p "Give expression example: 42+6 " calc
read NUM1 NUM2 <<<${calc//[^0-9]/ }
read oper <<<${calc//[0-9]/}

case $oper in
    \+)
    echo "$NUM1 plus $NUM2 equals $NUM1+$NUM2"
    ;;
    \-)
    echo "$NUM1 minus $NUM2 equals $NUM1-$NUM2"
    ;;
    \*)
    echo "$NUM1 multiplied by $NUM2 equals $NUM1*$NUM2"
    ;;
    \/)
    echo "$NUM1 divided by $NUM2 equals $NUM1/$NUM2"
    ;;
esac

相关问题