用shell实现根据输入分数判断等级
#!/bin/bash
#filename:ak
echo -n "enter your grades:"
read grade
if [ $grade -ge 0 -a $grade -le 59 ];then
echo "fail"
elif [ $grade -ge 60 -a $grade -le 100 ];then
echo "pass"
else
echo "error"
fi
运行结果:
用if语句ABCDE分级:
#!/bin/bash
#filename:zuoye
echo -n "enter your grades:"
read grade
if [ $grade -ge 0 -a $grade -le 59 ];then
echo "E"
elif [ $grade -ge 60 -a $grade -le 69 ];then
echo "D"
elif [ $grade -ge 70 -a $grade -le 79 ];then
echo "C"
elif [ $grade -ge 80 -a $grade -le 89 ];then
echo "B"
elif [ $grade -ge 90 -a $grade -le 100 ];then
echo "A"
else
echo "error"
fi
运行结果:
用case语句分级
#!/bin/bash
#filename:case
echo -n "enter your grades:"
read grade
case $grade in
9[0-9]|100) echo A;;
8[0-9]) echo B;;
7[0-9]) echo C;;
6[0-9]) echo D;;
[0-5][0-9]|[0-9]) echo E;;
*) echo ERROR;;
esac
运行结果: