挂着linuxfan的名号做了多年的资深伪非,若不是因为接触oracle db,恐怕就要伪非到底了……在重拾linux的过程中,鸟哥的私房菜让我受益颇多,这位来自台湾的帅锅在写书方面确实很有一套,依我看,维护世界和平的重任就交给他了。
此次练习参照《鸟哥私房菜基础篇V2》P283中的习题2,另外还加入了日期合法值检验的功能,若存在不合理之处,望不吝指教。
# !bin/bash
# Program:
# Tring to calculate your birthday at how many days.
# History:
# 2009/02/12 Old Yang Second Release
# 2008/11/10 Old Yang First Release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH# Part 1
echo -e "\nThis program will try to calculate :"
echo -e "How many days about your birthday...\n"# Part 2
until [ "$datebirth" == "q" ]
do
{
read -p "Please input your birthday (YYYYMMDD ex>20061116), or input q to quit: " datebirth
date_b=`echo $datebirth | grep '[0-9]\{8\}'`
declare -i date_y=`echo $datebirth | cut -c 1-4`
declare -i date_ly1=`expr $date_y % 4` # 闰年判断变量之被4整除
declare -i date_ly2=`expr $date_y % 100` # 闰年判断变量之被100整除
declare -i date_ly3=`expr $date_y % 400` # 闰年判断变量之被400整除
declare -i date_ty=`date +%Y%m%d | cut -c 1-4` # 获取今年的年份
date_m=`echo $datebirth | cut -c 5-6`
declare -i date_mv=`echo $datebirth | cut -c 5-6`
date_bm=`echo $date_m | egrep '01|03|05|07|08|10|12'`
date_sm=`echo $date_m | egrep '04|06|09|11'`
date_2m=`echo $date_m | egrep '02'`
declare -i date_d1=`echo $datebirth | cut -c 7`
declare -i date_d2=`echo $datebirth | cut -c 8`# Part 2-1 定义一个日期合法值检验的function
function judge()
{# 检验是否为今年
if [ "$date_b" != "" ] && [ $date_y -ne $date_ty ] && [ "$datebirth" != "q" ]; then
echo -e "\nYou must input the date in this year.\n"
exit 1
fi# 检验月份
if [ "$date_b" != "" ] && [ "$datebirth" != "q" ]; then
if [ $date_mv -eq 00 ] || [ $date_mv -gt 12 ]; then
echo -e "\nYou must input valid month.\n"
exit 1
fi
fi# 检验日期
if [ $date_d1 -eq 0 ] && [ $date_d2 -eq 0 ]; then
echo -e "\nYou must input valid date.\n"
exit 1
fi# 检验大月日期
if [ "$date_b" != "" ] && [ "$datebirth" != "q" ] && [ "$date_bm" != "" ];then
if [ $date_d1 -gt 3 ]; then
echo -e "\nYou must input valid date in the odd months.\n"
exit 1
fi
if [ $date_d1 -eq 3 -a $date_d2 -gt 1 ]; then
echo -e "\nYou must input valid date in the odd months.\n"
exit 1
fi
fi# 检验小月日期
if [ "$date_b" != "" ] && [ "$datebirth" != "q" ] && [ "$date_sm" != "" ]; then
if [ $date_d1 -gt 3 ];then
echo -e "\nYou must input valid date in the no-odd months.\n"
exit 1
fi
if [ $date_d1 -eq 3 -a $date_d2 -gt 0 ]; then
echo -e "\nYou must input valid date in the no-odd months.\n"
exit 1
fi
fi# 检验闰年的日期
if [ "$date_b" != "" ] && [ "$datebirth" != "q" ] && [ "$date_2m" != "" ]; then
if [ $date_d1 -gt 2 ]; then
echo -e "\nYou must input valid date in leap year.\n"
exit 1
fi
if [ $date_d1 -eq 2 ]; then
if [ $date_ly1 -ne 0 ] && [ $date_ly2 -eq 0 ] && [ $date_d2 -gt 8 ]; then
echo -e "\nYou must input valid date in leap year.\n"
exit 1
fi
if [ $date_ly3 -ne 0 ] && [ $date_d2 -gt 8 ]; then
echo -e "\nYou must input valid date in leap year.\n"
exit 1
fi
fi
fi}
# Part 2-2
if [ "$date_b" == "" ] && [ "$datebirth" != "q" ]; then
echo -e "\nError: The format of date must be (YYYYMMDD ex>20061116)\n"
fijudge;
if [ "$date_b" != "" ] && [ "$datebirth" != "q" ]; then
declare -i date_dem=`date --date="$date_b" +%s`
declare -i date_now=`date +%s`
declare -i date_total=$(($date_dem-$date_now))
declare -i date_b=$(($date_total/60/60/24))
{
if [[ $date_total -lt 0 ]]; then
echo -e "\nYou enjoyed your birthday before $((-1*$date_b)) days ago.\n"
exit 0
else
echo -e "\nYou will enjoy your birthday after $date_b days.\n"
exit 0
fi
}
fi}
done
well done
Aaron —— 2009年02月13日@11:32