bash - expr 显示表达式而不是求解它

这是我的 shell 脚本,

#! /bin/sh
# basic calculator
echo "Please input your choice"
printf " 1.Addition \n 2.SUbstraction \n 3.Multiplication \n 4.Division\n"
read choice
case "$choice" in
    1) echo "Enter number 1:";read n1;echo "Enter number 2:";read n2;t=$(expr "$n1"+"$n2");echo "$n1+$n2=$t";;
    2) echo "Enter number 1:";read n1;echo "Enter number 2:";read n2;t='expr $n1-$n2';echo "$n1-$n2=$t";;       
    3) echo "Enter number 1:";read n1;echo "Enter number 2:";read n2;t='expr $n1\*$n2';echo "$n1*$n2=$t";;
    4) echo "Enter number 1:";read n1;echo "Enter number 2:";read n2;t='expr $n1/$n2';echo "$n1/$n2=$t";;
esac

这是我的输出,

Script started on Sunday 08 November 2015 12:05:21 PM IST
Please input your choice
 1.Addition 
 2.SUbstraction 
 3.Multiplication 
 4.Division
1
Enter number 1:
5
Enter number 2:
6
5+6=5+6

问题是我的 expr 实际上并没有解决表达式

最佳答案

空格很重要:

$ expr 5+6
5+6
$ expr 5 + 6       
11 

要进行算术运算,您需要给 expr 3 个不同的参数。

https://stackoverflow.com/questions/33591458/

相关文章:

r - 将 3D 表面拟合到点数据集 [R]

php - FILTER_SANITIZE_NUMBER_INT 不将字符串转换为 int

windows - 使用 vmware 机器进行内核调试

three.js - 是否可以在 Three.js 中使用带缓冲几何的三角形带?

sql - 基于两个表的select CASE语句

python - 统一码编码错误 : 'ascii' codec can't encode char

c# - 在列表中查找一个属性包含重复值的对象

azure - 单个 Azure Web App 的多个实例是否共享单个磁盘?

php - 检查字符串中是否存在多个单词之一?

reactjs - 我如何在 react 中合并两个组件