shell脚本判断闰年并显示星期几
0x00 前言
作为shell编程新手,为了熟悉shell语法和编程风格,编写了这个脚本,这里没有做异常处理,所以输入字母时会报错,但是不影响脚本执行,以后改进。该脚本实现的功能有
1.判断输入日期是否合法
2.判断平闰年
3.显示输入的日期是星期几
0x01 判断日期是否合法
existDayInMonth(){
year="$1"
day="$3"
if [ "$year" -lt 1582 -o "$year" -gt 9999 ];then
echo -e "----------------------请输入1582-9999的年份!----------------------\n"
else
case $2 in
01|1|03|3|05|5|07|7|08|8|10|12)
if [ "$day" -lt 1 -o "$day" -gt 31 ];then
echo -e "----------------------$2月$3日是无效的日期----------------------\n"
fi
;;
02|2)
if [ "$day" -lt 1 -o "$day" -gt 28 ];then
echo -e "----------------------$2月$3日是无效的日期----------------------\n"
fi
;;
04|4|06|6|09|9|11)
if [ "$day" -lt 1 -o "$day" -gt 30 ];then
echo -e "----------------------$2月$3日是无效的日期----------------------\n"
fi
;;
*)
echo -e "----------------------日历表中没有$2这个月----------------------\n"
;;
esac
fi
}
0x01 判断是否为闰年
isLeapYear(){
year="$1"
if [ "$((year % 4))" -eq 0 -a "$((year % 100))" -ne 0 ];then
echo -e "******$year年是闰年!"
elif [ "$((year % 400))" -eq 0 ];then
echo -e "******$year年是闰年!"
else
echo -e "******$year年是平年!"
fi
}
0x02 判断日期是星期几-蔡勒公式
whatDay(){
yearstr="$1"
c=${yearstr:0:2} #年份前两位数
y=${yearstr:0-2:2} #年份后两位数
m="$2" #月(m的取值范围为3至14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算)
d="$3" #日
case $2 in
01|1|02|2)
y=`expr $y - 1`
m=`expr $m + 12`
;;
*)
;;
esac
#计算W所得的数值对应的星期:0-星期日;1-星期一;2-星期二;3-星期三;4-星期四;5-星期五;6-星期六
week=$(($((26*$((m +1))))/10))
wl=`expr $y + $y / 4 + $c / 4 - 2 \* $c + $week + $d - 1`
if [ "$wl" -ge 0 ]
then
W=$((wl % 7))
else
W=$((wl % 7 + 7))
fi
case $W in
0) echo -e "******$year年$m月$d日是星期日!!" ;; 1) echo -e "******$year年$m月$d日是星期一!!" ;;
2) echo -e "******$year年$m月$d日是星期二!!" ;; 3) echo -e "******$year年$m月$d日是星期三!!" ;;
4) echo -e "******$year年$m月$d日是星期四!!" ;; 5) echo -e "******$year年$m月$d日是星期五!!" ;;
6) echo -e "******$year年$m月$d日是星期六!!" ;; *) ;;
esac
}
0x03 完整代码
#!/bin/bash
# 功能:判断输入的日期是否为闰年,并显示星期几
# 作者:Peithon
# 时间: 2018-10-8
#判断日期是否合法
existDayInMonth(){
year="$1"
day="$3"
if [ "$year" -lt 1582 -o "$year" -gt 9999 ];then
echo -e "----------------------请输入1582-9999的年份!----------------------\n"
return 0
else
case $2 in
01|1|03|3|05|5|07|7|08|8|10|12)
if [ "$day" -lt 1 -o "$day" -gt 31 ];then
echo -e "----------------------$2月$3日是无效的日期----------------------\n"
return 0
else
return 1
fi
;;
02|2)
if [ "$day" -lt 1 -o "$day" -gt 28 ];then
echo -e "----------------------$2月$3日是无效的日期----------------------\n"
return 0
else
return 1
fi
;;
04|4|06|6|09|9|11)
if [ "$day" -lt 1 -o "$day" -gt 30 ];then
echo -e "----------------------$2月$3日是无效的日期----------------------\n"
return 0
else
return 1
fi
;;
*)
echo -e "----------------------日历表中没有$2这个月----------------------\n"
return 0
;;
esac
fi
}
#判断是否为闰年
isLeapYear(){
year="$1"
if [ "$((year % 4))" -eq 0 -a "$((year % 100))" -ne 0 ];then
echo -e "******$year年是闰年!"
elif [ "$((year % 400))" -eq 0 ];then
echo -e "******$year年是闰年!"
else
echo -e "******$year年是平年!"
fi
}
#判断日期是星期几-蔡勒公式
whatDay(){
yearstr="$1"
c=${yearstr:0:2} #年份前两位数
y=${yearstr:0-2:2} #年份后两位数
m="$2" #月(m的取值范围为3至14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算)
d="$3" #日
case $2 in
01|1|02|2)
y=`expr $y - 1`
m=`expr $m + 12`
;;
*)
;;
esac
#计算W所得的数值对应的星期:0-星期日;1-星期一;2-星期二;3-星期三;4-星期四;5-星期五;6-星期六
week=$(($((26*$((m +1))))/10))
wl=`expr $y + $y / 4 + $c / 4 - 2 \* $c + $week + $d - 1`
if [ "$wl" -ge 0 ]
then
W=$((wl % 7))
else
W=$((wl % 7 + 7))
fi
case $W in
0) echo -e "******$year年$m月$d日是星期日!!" ;; 1) echo -e "******$year年$m月$d日是星期一!!" ;;
2) echo -e "******$year年$m月$d日是星期二!!" ;; 3) echo -e "******$year年$m月$d日是星期三!!" ;;
4) echo -e "******$year年$m月$d日是星期四!!" ;; 5) echo -e "******$year年$m月$d日是星期五!!" ;;
6) echo -e "******$year年$m月$d日是星期六!!" ;; *) ;;
esac
}
while true
do
if read -t 30 -p "**年份为四位数(1582以后)**输入年月日(例如1582.10.15):" dates
then
str="$dates";
arr=(${str//./ }); #将输入的日期进行分割
echo -e "\n您输入的是${arr[0]}年${arr[1]}月${arr[2]}日,判断结果如下:"
existDayInMonth ${arr[0]} ${arr[1]} ${arr[2]}
if [ $? -eq 1 ];then
isLeapYear ${arr[0]}
whatDay ${arr[0]} ${arr[1]} ${arr[2]}
fi
else
echo -e "\n30s未输入已自动退出"
exit 1
fi
done
运行测试,vi filename.sh
,复制粘贴上面代码,chmod +x filename.sh
赋权,使用./filename.sh
执行就OK了。
0x04 参考文档
1.Shell 基本运算符
2.维基蔡勒公式
3.shell字符串截断、切片与默认值
0x05 总结
编写这个脚本查了很多资料,因为对shell编程也是才入门,虽然代码写的不好,但是功能基本实现了,可能会有一些缺陷(有些小细节可能没注意到),但是也是自己成长的见证,所以记录下来,方便自己查看。