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

标签 bash shell unix

这是我的 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 个不同的参数。

关于bash - expr 显示表达式而不是求解它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33591458/

相关文章:

python - 启动一个新进程并读取该进程的输出

linux - Bash 限制父脚本命令行参数传递给子脚本参数

linux - 在 shell 脚本中的文件中插入带有特殊字符(正斜杠)的行

bash - 从字符串中正确读取引用/转义的参数

bash - 从 12GB 文件中删除特定行

bash - 来自反引号的多字参数(命令替换)

linux - 使用电子表格中的数据标记音频文件

bash - 查找所有包含文本 "example.html"的文件并替换为 "example.php"仅当文件名中没有空格时才有效

unix - 检查可执行文件是否有效

bash - 如何使用域列表作为输入来更新配置文件?