shell打印给定日期的日历

所属分类: 脚本专栏 / linux shell 阅读数: 878
收藏 0 赞 0 分享

学习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 ""

执行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

linux链接编译详解

通过自己编写Makefile文件来实现多目标文件的链接,其实就是将gcc 编译命令写入到Makefile文件,通过执行make 指令来实现编译。这样我不需要每次都敲入gcc -指令。直接输入make就OK了。
收藏 0 赞 0 分享

利用linux的timerfd_create实现计时器示例分享

这篇文章主要介绍了利用linux的timerfd_create实现计时器示例,需要的朋友可以参考下
收藏 0 赞 0 分享

shell脚本监控linux系统内存使用情况的方法(不使用nagios监控linux)

在没有nagios监控软件的情况下,只要服务器能上互联网,就可通过发邮件的方式来提醒管理员系统内存的使用情况
收藏 0 赞 0 分享

linux使用管道命令执行ps获取cpu与内存占用率

这篇文章主要介绍了linux使用管道命令执行ps获取cpu与内存占用率的示例,需要的朋友可以参考下
收藏 0 赞 0 分享

shell字符串操作详解

这篇文章主要介绍了shell字符串操作详解的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell 逻辑运算符、逻辑表达式详细介绍

shell的逻辑运算符 涉及有以下几种类型,因此只要适当选择,可以解决我们很多复杂的判断,达到事半功倍效果
收藏 0 赞 0 分享

ftp自动上传脚本分享

调试设备时,往往需要反复将新编译的二进制通过ftp上传到目标机的指定目录下,此工作的重复性过多,考虑将操作写为shell脚本
收藏 0 赞 0 分享

shell脚本学习指南[一](Arnold Robbins & Nelson H.F. Beebe著)

这篇文章主要介绍了shell脚本学习指南[一]Arnold Robbins & Nelson H.F. Beebe著,需要的朋友可以参考下
收藏 0 赞 0 分享

shell脚本学习指南[二](Arnold Robbins & Nelson H.F. Beebe著)

这篇文章主要介绍了shell脚本学习指南[二](Arnold Robbins & Nelson H.F. Beebe著),需要的朋友可以参考下
收藏 0 赞 0 分享

shell脚本学习指南[三](Arnold Robbins & Nelson H.F. Beebe著)

这篇文章主要介绍了shell脚本学习指南[三](Arnold Robbins & Nelson H.F. Beebe著),需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多