LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
123
返回列表 发新帖
楼主: Kingo

谁可以帮忙写下面这个shell程序?谢谢先!!

[复制链接]
 楼主| 发表于 2003-11-20 21:24:52 | 显示全部楼层
最初由 home 发表
不懂。不是显示小数。
FREE# echo $(($a/2))
1
FREE# echo $((-$a/2))
-1
FREE# echo $a
3

$ echo "scale=2; 1/2" | bc
.50
发表于 2003-11-20 21:33:02 | 显示全部楼层
最初由 Kingo 发表
$ echo "scale=2; 1/2" | bc
.50


你那样也是可以的:p :p

佩服。。你能解释一下吗?
 楼主| 发表于 2003-11-20 21:37:05 | 显示全部楼层
我只知道这个语句如何理解,但是具体应用我还是不大会。
scale=2 ----显示两位小数。
bc      ----man过之后就知道了,用来计算的。
发表于 2003-11-20 21:51:48 | 显示全部楼层
thanks~~~
发表于 2003-11-27 18:16:24 | 显示全部楼层

呵呵,俺也写了一个学习学习,,,

脚本:
$cat ./count.sh
#!/bin/sh
profit=
while [ -z "$profit" ];
do
   printf "%s" "lease input a number:"
   read profit
   nums=`expr $profit + 0 > /dev/null 2>&1`
   ###似乎没什么好方法检验输入数字是否合法,所以就用了这个笨方法
   if [ $? -ne 0 ];then
      echo "Input invalid,please input again!!"
      profit=
   fi   ##判断输入是否合法
done

if [ $profit -le 100000 ]; then
   bonus=`expr $profit \* 10 / 100`
elif [ $profit -le 200000 ]; then
   bonus=`expr $profit \* 75 / 1000 + 2500`
elif [ $profit -le 400000 ]; then
   bonus=`expr $profit \* 5 / 100 + 7500`
elif [ $profit -le 600000 ]; then
   bonus=`expr $profit \* 3 / 100 + 19500`
elif [ $profit -le 800000 ]; then
   bonus=`expr $profit \* 15 / 1000 + 24500`
else
   bonus=`expr $profit / 100 + 29500`
fi
echo "rofit is $profit"
echo "Bonus is $bonus"


大同小异,反正这个也比较简单,
对于shell来说,数学运算得到的小数点以后的部分都被舍去了!!

输出:
$ ./count.sh
Please input a number:120050
Profit is 120050
Bonus is 11503

$ ./count.sh
Please input a number:ssss2
Input invalid,please input again!!
Please input a number:

$ ./count.sh
Please input a number:3312 23
Input invalid,please input again!!
Please input a number:
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表