shell打印给定日期的日历
2019-07-31 12:02
1716 查看
学习shell有一段时间了,一直没有机会练手,看到同事发了一张照片,控制台显示了当月的日历,是用Python实现的,感觉挺好玩,所以准备用shell来实现一个,搞了一下午,终于搞定。
打印本月的日期
#! /bin/bash #设置字体颜色 tiffcolor="\033[0;35m" menucolor="\033[0;33m" todaycolor="\033[0;35;44m" start="\033[0m" #计算各个日期 month=`date +%m` day=`date +%d` year=`date +%Y` weekday=`date -d "$year-$month-01" +%w` nextmonth=`expr $month + 1` today=`date +%d` #计算本月有多少天 differ=$(( ($(date -d "$year-$nextmonth-01" +%s) - $(date +%s))/(24*60*60) )) days=`expr $differ + $day` #打印标题 echo -en "${menucolor}" echo -en "\t $year $month\n" echo "SUN MON TUE WEN THU FRI SAT" echo -en "${start}" #打印空格 if [ $weekday -ne 0 ];then for((i=1;i<=$weekday;i++)) do echo -n " " echo -n " " done fi #打印日期 for((i=1;i<=$days;i++)) do printf "%s" " " echo -en "${tiffcolor}" #今天高亮显示 if [ $today -eq $i ];then echo -en "${todaycolor}" fi printf "%2d" $i echo -en "${start}" echo -en " " if [ $((($weekday+$i)%7)) == 0 ];then echo "" fi # printf "%3d " $i done echo ""
执行结果:
扩展:给定任意日期,打印当月的日期
#! /bin/bash #date=$1 tiffcolor="\033[0;35m" menucolor="\033[0;33m" todaycolor="\033[0;35;44m" start="\033[0m" if [ $# -ne 1 ];then echo "plz input the date" exit 1 fi date=$1 count=`echo $date |grep -o '-'|wc -l` if [ $count -ne 2 ];then echo "plz input correct date" exit 1 fi year=`echo $date|cut -d "-" -f 1` month=`echo $date|cut -d "-" -f 2` day=`echo $date|cut -d "-" -f 3` expr $year + $month + $day + 0 &>/dev/null if [ $? -ne 0 ];then echo "plz input a correct date" exit 1 elif [ $month -gt 12 -o $month -eq 0 ];then echo "plz input the month between 1 and 12" exit 1 elif [ $day -gt 31 -o $day -eq 0 ];then echo "plz input the day between 1 and 31" exit 1 fi #nextmonth=$(( $month + 1)) #month=`date -d "$date" +%m` #day=`date -d "$date" +%d` #year=`date -d "$date" +%Y` weekday=`date -d "$year-$month-01" +%w` if [ $month -eq 12 ];then newmonth=1 newyear=`expr $year + 1` else newyear=$year #nextmonth= expr $month + 1 newmonth=`expr $month + 1` fi days=$(( ($(date -d "${newyear}-${newmonth}-01" +%s) - $(date -d "$year-$month-01" +%s))/(24*60*60) )) #echo $days echo -en "${menucolor}" echo -en "\t $year $month\n" echo "SUN MON TUE WEN THU FRI SAT" echo -en "${start}" if [ $weekday -ne 0 ];then for((i=1;i<=$weekday;i++)) do echo -n " " echo -n " " done fi for((i=1;i<=$days;i++)) do printf "%s" " " echo -en "${tiffcolor}" if [ $day -eq $i ];then echo -en "${todaycolor}" fi printf "%2d" $i echo -en "${start}" echo -en " " if [ $((($weekday+$i)%7)) == 0 ];then echo "" fi # printf "%3d " $i done echo ""
执行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- 使用shell实现打印给定日期的日历
- Shell打印日期列表
- c++: 格式化打印日历一个月的所有日期
- python 获取某个月的全部日期,打印日历的简单算法
- [学习笔记]打印出该月的日期表格,日历形式
- 用C++编一程序,先输出一行sun mon tue wed thu fri fri,接着使用右对齐打印出日期,像日历那样
- 输入的日期用calendar来打印出这个月的日历(复习)
- 编写程序,利用GregorianCalendar类,输入日期(yyyy-MM-dd),打印当前月份的日历。
- python根据给定的年月日以数字形式打印日期
- 百练 打印日历(日期问题)
- Shell打印日期范围的列表日期
- shell脚本打印日期时间
- shell截取指定日期的nginx log打印出来
- 给定某年日期及该年第一天是星期几然后显示该年的日历
- 记录cu上shell比赛的第6题:输入日期,打印天数
- shell日期处理:给定日期推周几日期、求上周周几日期
- 给定两个日历对象计算这连个日期相差多少年、月、日、天、时、分、秒
- 记录cu上shell比赛的第6题:输入日期,打印天数
- 打印给定年月的日历
- 接收控制台输入的日期,打印日历