shell打印給定日期的日歷
學(xué)習(xí)shell有一段時(shí)間了,一直沒(méi)有機(jī)會(huì)練手,看到同事發(fā)了一張照片,控制臺(tái)顯示了當(dāng)月的日歷,是用Python實(shí)現(xiàn)的,感覺(jué)挺好玩,所以準(zhǔn)備用shell來(lái)實(shí)現(xiàn)一個(gè),搞了一下午,終于搞定。
打印本月的日期
#! /bin/bash #設(shè)置字體顏色 tiffcolor="\033[0;35m" menucolor="\033[0;33m" todaycolor="\033[0;35;44m" start="\033[0m" #計(jì)算各個(gè)日期 month=`date +%m` day=`date +%d` year=`date +%Y` weekday=`date -d "$year-$month-01" +%w` nextmonth=`expr $month + 1` today=`date +%d` #計(jì)算本月有多少天 differ=$(( ($(date -d "$year-$nextmonth-01" +%s) - $(date +%s))/(24*60*60) )) days=`expr $differ + $day` #打印標(biāo)題 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 ""
執(zhí)行結(jié)果:
擴(kuò)展:給定任意日期,打印當(dāng)月的日期
#! /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 ""
執(zhí)行結(jié)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Linux下統(tǒng)計(jì)當(dāng)前文件夾下的文件個(gè)數(shù)、目錄個(gè)數(shù)
這篇文章主要介紹了Linux下統(tǒng)計(jì)當(dāng)前文件夾下的文件個(gè)數(shù)、目錄個(gè)數(shù),本文使用ls命令配合管理、grep命令實(shí)現(xiàn)統(tǒng)計(jì)需求,需要的朋友可以參考下2014-10-10Shell腳本傳遞參數(shù)的4種方式實(shí)例說(shuō)明
Shell腳本是一種命令語(yǔ)言,可以用于自動(dòng)化執(zhí)行各種任務(wù),在腳本中,我們可以通過(guò)參數(shù)來(lái)傳遞信息,本文將介紹如何在shell腳本中傳遞參數(shù),包括位置參數(shù)、特殊變量、環(huán)境變量和命名參數(shù),需要的朋友可以參考下2023-06-06Linux中的host命令應(yīng)用實(shí)例詳解
這篇文章主要介紹了Linux中的host命令應(yīng)用舉例的相關(guān)資料,需要的朋友可以參考下2017-07-07Linux Shell 數(shù)組的創(chuàng)建及使用技巧
這篇文章主要介紹了Linux Shell 數(shù)組的創(chuàng)建及使用技巧,本文講解了數(shù)組定義、數(shù)組讀取與賦值以及特殊使用,需要的朋友可以參考下2015-07-07linux重啟和關(guān)閉系統(tǒng)命令的寫(xiě)法
這篇文章給大家介紹了linux重啟和關(guān)閉系統(tǒng)命令的寫(xiě)法,然后在文中給大家分享了linux五個(gè)重啟命令,感興趣的朋友一起看看吧2017-10-10Shell(())實(shí)現(xiàn)對(duì)整數(shù)進(jìn)行數(shù)學(xué)運(yùn)算
這篇文章主要介紹了Shell(())實(shí)現(xiàn)對(duì)整數(shù)進(jìn)行數(shù)學(xué)運(yùn)算,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03