国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Linux > 正文

shell打印給定日期的日歷

2019-10-26 19:00:08
字體:
供稿:網(wǎng)友

學(xué)習(xí)shell有一段時間了,一直沒有機(jī)會練手,看到同事發(fā)了一張照片,控制臺顯示了當(dāng)月的日歷,是用Python實(shí)現(xiàn)的,感覺挺好玩,所以準(zhǔn)備用shell來實(shí)現(xiàn)一個,搞了一下午,終于搞定。

打印本月的日期

#! /bin/bash#設(shè)置字體顏色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`#打印標(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 ];thenfor((i=1;i<=$weekday;i++))do  echo -n "  "  echo -n " "donefi#打印日期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 " $idoneecho ""

執(zhí)行結(jié)果:

擴(kuò)展:給定任意日期,打印當(dāng)月的日期

#! /bin/bash#date=$1tiffcolor="/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 1fidate=$1count=`echo $date |grep -o '-'|wc -l`if [ $count -ne 2 ];then  echo "plz input correct date"  exit 1fiyear=`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/nullif [ $? -ne 0 ];then  echo "plz input a correct date"  exit 1elif [ $month -gt 12 -o $month -eq 0 ];then  echo "plz input the month between 1 and 12"  exit 1elif [ $day -gt 31 -o $day -eq 0 ];then  echo "plz input the day between 1 and 31"  exit 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 "${newyear}-${newmonth}-01" +%s) - $(date -d "$year-$month-01" +%s))/(24*60*60) )) #echo $daysecho -en "${menucolor}"echo -en "/t  $year $month/n"echo "SUN MON TUE WEN THU FRI SAT"echo -en "${start}"if [ $weekday -ne 0 ];thenfor((i=1;i<=$weekday;i++))do  echo -n "  "  echo -n " "donefifor((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 " $idoneecho ""            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 湟中县| 左云县| 天全县| 法库县| 山东省| 九龙城区| 沽源县| 棋牌| 息烽县| 宁波市| 哈密市| 法库县| 星座| 大港区| 宣城市| 乐安县| 延庆县| 临猗县| 蒙自县| 梓潼县| 枣阳市| 金华市| 沈阳市| 璧山县| 郎溪县| 昭觉县| 秦安县| 桑日县| 武功县| 海城市| 翼城县| 靖江市| 友谊县| 平原县| 江陵县| 安泽县| 芦溪县| 宁陕县| 合肥市| 德化县| 兰考县|