shell 分数等级_Shell分数分级小程序

Shell编程,在bash下编写一个输入分数然后判定级别,整数比较容易实现,但是要增加浮点数处理就比较麻烦,以下是我写的一个小程序,有兴趣可以看看。

#! /bin/bash

function deal() #比较浮点数,$1>=$2 返回1 其他返回0 (网上资料)

{

echo "$1>=$2"|bc

}

function max() #同上

{

echo "$1>$2"|bc

}

function matchInt() #正则表达式匹配整型数

{

echo $1 | egrep '^[0-9]+$'

}

function matchFloat() #同上,浮点

{

echo $1 | egrep '^[0-9]+\.[0-9]+$'

}

function matchIntF() #正则表达式匹配负整型数

{

echo $1 | egrep '^-[0-9]+$'

}

function matchFloatF() #同上,负浮点

{

echo $1 | egrep '^-[0-9]+\.[0-9]+$'

}

function analyze() #配对函数

{

if [ $(deal 0 ${score}) -eq 0 -a $(deal ${score} 100) -eq 0 -o $(max ${score} 100) -eq 0 ]

then

if [ $(deal ${score} 60) -eq 0 ]

then echo "failed!"

elif [ $(deal ${score} 70) -eq 0 ]

then echo "passed!"

elif [ $(deal ${score} 80) -eq 0 ]

then echo "mediun!"

elif [ $(deal ${score} 90) -eq 0 ]

then echo "good!"

else echo "excellent!"

fi

else echo "input a wrong score, please input a score in 0~100!"

fi

}

echo "Enter a score"

loop=yes

while [ ${loop} ]

do

read score

if [ ${score} = quit ]

then exit

elif [ $(matchInt ${score}) ] #这里想用-o连接下面的,不过好像不行

then analyze ${score}

elif [ $(matchFloat ${score}) ]

then analyze ${score}

elif [ $(matchIntF ${score}) ]

then echo "please input a positive score!"

elif [ $(matchFloatF ${score}) ]

then echo "please input a positive score!"

else echo "plese input a integer or float!"

fi

done

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2010-11-02 20:36

浏览 1031

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值