shell打印给定日期的日历

 更新时间:2019年07月31日 12:02:27   作者:小他529  
这篇文章主要为大家详细介绍了shell打印给定日期的日历,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

学习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 Shell脚本里面把一个数组传递到awk内部进行处理

    linux Shell脚本里面把一个数组传递到awk内部进行处理

    这篇文章主要介绍了linux Shell脚本里面把一个数组传递到awk内部进行处理,需要的朋友可以参考下
    2016-12-12
  • 使用shell脚本来给mysql加索引的方法

    使用shell脚本来给mysql加索引的方法

    今天小编就为大家分享一篇关于使用shell脚本来给mysql加索引的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-04-04
  • centos 设置定时任务执行指定脚本的方法

    centos 设置定时任务执行指定脚本的方法

    下面小编就为大家带来一篇centos 设置定时任务执行指定脚本的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • 实现自动清除日期目录shell脚本实例代码

    实现自动清除日期目录shell脚本实例代码

    这篇文章主要介绍了实现自动清除日期目录shell脚本实例代码的相关资料,需要的朋友可以参考下
    2017-04-04
  • 修改Apache配置指定php配置文件php.ini的位置方法

    修改Apache配置指定php配置文件php.ini的位置方法

    下面小编就为大家带来一篇修改Apache配置指定php配置文件php.ini的位置方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • Shell脚本逐行读取文本文件(不改变文本格式)

    Shell脚本逐行读取文本文件(不改变文本格式)

    这篇文章主要介绍了Shell脚本逐行读取文本文件,本文着重探讨不改变文本格式的方法读取出文件内容,需要的朋友可以参考下
    2015-04-04
  • shell脚本引用外部变量的两种方法

    shell脚本引用外部变量的两种方法

    本文主要介绍了shell脚本引用外部变量的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • Shell正则表达式学习笔记

    Shell正则表达式学习笔记

    正则表达式是一种表示方式,可以让你查找匹配特定的准则的文本,例如以字母a开头的文件,学好正则表达式将使以后的文本处理变得方便,快速,本文给大家介绍shell正则表达式学习笔记,对shell正则表达式相关知识感兴趣的朋友一起学习吧
    2015-12-12
  • ansible执行shell脚本的方法

    ansible执行shell脚本的方法

    本文主要介绍了ansible执行shell脚本的方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • 使用ubuntu搭建公网个人邮件服务器(基于postfix,dovecot,mysql)

    使用ubuntu搭建公网个人邮件服务器(基于postfix,dovecot,mysql)

    这篇文章主要介绍了基于ubuntu搭建公网个人邮件服务器(基于postfix,dovecot,mysql),免费的邮箱每天发信数量是有限制的,所以呢就想着搭建一个自己的邮件服务器,需要的朋友可以参考下
    2019-06-06

最新评论