自己第一次写shell脚本,边学边仿照这写的,展现出来以供参考
#!/bin/bash
echo "***zodiac and constellation***(Press q exit)"
echo -n "Please input yout birthday(ex:20190327): "
while read input
do
if [ $input == 'q' ]
then break
fi
len=${#input}
#echo "--->$len"
if [ $len != 8 ]
then
echo "input error"
continue
fi
y=${input: 0: 4}
year=`expr $y`
m=${input: 4: 2}
month=`expr $m`
d=${input: 6}
day=`expr $d`
mdc=${input: 4}
md=`expr $mdc`
echo "$year/$month/$day"
if [ "$month" -lt "01" ] || [ "$month" -gt "12" ]
then
echo "month error: $month"
echo -n "Please input yout birthday(ex:20190327): "
continue
fi
if [ "$day" -lt "1" ] || [ "$day" -gt "31" ]
then
echo "day error: $day"
echo -n "Please input yout birthday(ex:20190327): "
continue
fi
mod=`expr $year % 12`
case $mod in
0) echo "You were born in the year of Monkey."
;;
1) echo "You were born in the year of Rooster."
;;
2) echo "You were born in the year of Dog."
;;
3) echo "You were born in the year of Boar."
;;
4) echo "You were born in the year of Rat."
;;
5) echo "You were born in the year of Ox."
;;
6) echo "You were born in the year of Tiger."
;;
7) echo "You were born in the year of Hare."
;;
8) echo "You were born in the year of Dragon."
;;
9) echo "You were born in the year of Snake."
;;
10) echo "You were born in the year of Horse."
;;
11) echo "You were born in the year of Sheep."
;;
esac
if [ "$md" -le "0120" ]
then echo "Capricorn "
elif [ "$md" -ge "0121" ] && [ "$md" -le "0219" ]
then echo "Aquarius"
elif [ "$md" -ge "0220" ] && [ "$md" -le "0320" ]
then echo "Pisces "
elif [ "$md" -ge "0321" ] && [ "$md" -le "0410" ]
then echo "Aries "
elif [ "$md" -ge "0421" ] && [ "$md" -le "0521" ]
then echo "Taurus "
elif [ "$md" -ge "0522" ] && [ "$md" -le "0621" ]
then echo "Gemini "
elif [ "$md" -ge "0622" ] && [ "$md" -le "0722" ]
then echo "Cancer "
elif [ "$md" -ge "0723" ] && [ "$md" -le "0823" ]
then echo "Leo"
elif [ "$md" -ge "0824" ] && [ "$md" -le "0923" ]
then echo "Virgo "
elif [ "$md" -ge "0924" ] && [ "$md" -le "1023" ]
then echo "Libra "
elif [ "$md" -ge "1024" ] && [ "$md" -le "1122" ]
then echo "Scorpio "
elif [ "$md" -ge "1123" ] && [ "$md" -le "1221" ]
then echo "Sagittarius "
else echo "Capricorn "
fi
echo "Please input yout birthday(ex:20190327): "
done