shell练习题

回复 收藏
传递三个参数给脚本,第一个为整数,第二个为算术运算符,第三个为整数,将计算结果显示出来,要求保留两位精度。形如:
./calc.sh 5 / 2
2016-06-05 17:50 举报
已邀请:
0

kw是id

赞同来自:

更新:乘号如果被当作参数输入,默认占用四个参数位,所以这个脚本是不完美的,因为做不了乘法运算

#!/bin/bash
if [ $# -ne 3 ]
then
   echo "please input the first parameter number between 0 to 9"
   echo "please input the second parameter as one of below + - * / %"
   echo "please input the third parameter number between 0 to 9"
   exit 1
fi
a=`echo $1|sed 's/[0-9]//g'`
b=`echo $3|sed 's/[0-9]//g'`
if [ -n "$a" -o -n "$b" ]
then
   echo "please input number between 0 to 9"
   exit 1
fi
echo "scale=2;$1$2$3"|bc


0

陈洪良

赞同来自:

8 "*" 4

这样就可以做乘法运算了。

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: