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
分享到:
2010-11-02 20:36
浏览 1031
评论