一、输入两个数,实现排序
代码:
#!/bin/bash
read -p "please enter n m:" n m
if [ $n -gt $m ]
then
temp=$n
n=$m
m=$temp
fi
echo $n $m
运行结果
二、输入一个数判断是否水仙花数
代码:
echo narcissistic number
read -p "please enter num:" num
if [ $num -ge 100 -a $num -le 999 ]
then
unit=$((num%10))
decade=$((num/10%10))
hundreds=$((num/100))
sum=$((unit*unit*unit+decade*decade*decade+hundreds*hundreds*hundreds))
if [ $num -eq $sum ]
then
echo $num is narcissistic number
else
echo $num is not narcissistic number
fi
else
echo enetr error
fi
运行结果
三、输入成绩实现等级判断,A:90-100,B:80-89,C:70-79,D:0-69
代码
read -p "please enter score:" score
case $score in
9[0-9]|100)
echo A
;;
8[0-9])
echo B
;;
7[0-9])
echo C
;;
[0-6][0-9]|[0-9])
echo D
;;
*)
echo error
;;
esac
运行结果