shell列印給定日期的日曆

2022-09-26 14:06:24 字數 3204 閱讀 2179

學習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 "$"

echo -en "\t $year $month\n"

echo "sun mon tue wen thu fri sat"

echo -en "$"

#列印空格

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 "$"

#今天高亮顯示

if [ $today -eq $i ];then

echo -en "$"

fiprintf "%2d" $i

echo -en "$"

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

fidate=$1

count=`echo $date |grep -o '-'|wc -l`

if [ $count -ne 2 ];then

echo "plz input correct date"

exit 1

fiyear=`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"

1fi#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`

fidays=$(( ($(date -d "$-$-01" +%s) - $(date -d "$year-$mo" +%s))/(24*60*60) www.cppcns.com))

#echo $days

echo -en "$"

echo -en "\t $year $month\n程式設計客棧"

echo "sun mon tue wen thu fri sat"

echo -en "$"

if [ $weekday -ne 0 ];then

for((i=1;i<=$weekdawww.cppcns.comy;i++))

do echo -n " "

echo -n " "

done

fifor((i=1;i<=$days;i++))

do printf "%s" " "

echo -en "$"

if [ $day -eq $i ];then

echo -en "$"

fiprintf "%2d" $i

echo -en "$"

echo -en " "

if [ $((($weekday+$i)%7)) == 0 ];then

echo ""

fi# printf "%3d " $i

done

echo ""

執行結果:

本文標題: shell列印給定日期的日曆

本文位址:

查詢給定日期的月份

select substr to char add months to date 200901 yyyymm rownum 1 yyyymm 5,2 月 as month from eos dict entry where add months to date 200901 yyyymm rownu...

將給定日期增加n個月

將給定日期增加numday個月 function adddate dtdate,numday adddate 2008 01 01 replace g,2 adddate 5 5,2004 12 1 00 00 00 function adddate type,numday,dtdate retur...

如何實現給定日期的若干天以後的日期

這幾天突然有很多的人問這樣的問題,就是如何在php中實現在vb中的dateadd的函式,呵呵!這個可是問個正著。本來這個問題是 豆腐 去 華為 應聘的時候的乙個考試題,不過當時是用c 實現的。沒有想到這樣的大公司,竟 然用這樣的小兒科來考試 後來我沒有去,這兩天 應 的 運氣,用php重新 寫了這個...