作者:dunerunner
代码:
#!/bin/bash
# week
#QQ:82386723 E-mail:sc_fan@163.com
value=year
error()
{
echo $value error! please input again.
continue
}
while
read -p "Please input $value: " tmp
do
if [ "$tmp" ]; then
if [ `echo $tmp | awk '{if($0~/[^0-9]/) print "1"}'` ]; then
echo $value error! please input again.
continue
fi
else
error
fi
case $value in
year)
value=month
year=$tmp
continue
;;
month)
if [ $tmp -gt 0 -a $tmp -le 12 ]; then
value=day
month=$tmp
continue
else
error
fi
;;
day)
case $month in
1|3|5|7|8|10|12)
if [ $tmp -gt 0 -a $tmp -le 31 ]; then
day=$tmp
break
else
error
fi
;;
4|6|9|11)
if [ $tmp -gt 0 -a $tmp -le 30 ]; then
day=$tmp
break
else
error
fi
;;
2)
case $[year%4] in
0)
if [ $tmp -gt 0 -a $tmp -le 29 ]; then
day=$tmp
break
else
echo $value error! $year is leap year, input again.
continue
fi
;;
*)
if [ $tmp -gt 0 -a $tmp -le 28 ]; then
day=$tmp
break
else
error
fi
;;
esac
;;
esac
;;
esac
done
century=$[year/100+1]
declare -i tmp=0
for (( i = 0; i < $[year%28]; i++))
do if [ $[i%4] = 0 ]; then
tmp=$[tmp+366]
else
tmp=$[tmp+365]
fi
done
for (( i = 1; i < $month; i++))
do case $i in
1|3|5|7|8|10|12)
tmp=$[tmp+31]
;;
4|6|9|11)
tmp=$[tmp+30]
;;
2)
if [ $[year%4] = 0 ]; then
tmp=$[tmp+29]
else
tmp=$[tmp+28]
fi
;;
esac
done
tmp=$[(tmp+day)%7]
week=(Thursday Friday Saturday Sunday Monday Tuesday Wednesday)
week=${week[tmp]}
echo -e "\nCentury $century $year.$month.$day $week\n"
本人使用linux半年多了,水平不济可能有
比较罗嗦的语句,望各位不要见笑,还请大家多多指教。
上面的
脚本是我自己根据一些规律写出来的,加入了点判定,如果输入字母和符号等错误会提示,从公元0年以后的所有日期都可以算出
星期。