这是我的 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/