Bash
使用 ncal 或 cal 以特定格式顯示週數
當兩個命令都做你想做的一件事但兩者都不做時,你難道不喜歡它嗎?
這就是這樣
cal
做的。漂亮的格式。但缺少週數:$ cal January 2012 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
這就是這樣
ncal
做的。奇怪的格式,但有周數:$ ncal -w January 2012 Su 1 8 15 22 29 Mo 2 9 16 23 30 Tu 3 10 17 24 31 We 4 11 18 25 Th 5 12 19 26 Fr 6 13 20 27 Sa 7 14 21 28 1 2 3 4 5
我想要的那種輸出,實際上是和之間的
cal
雜交ncal -w
:$ cal --magik-calendar-week-option January 2012 Su Mo Tu We Th Fr Sa 1 1 2 3 4 5 6 7 2 8 9 10 11 12 13 14 3 15 16 17 18 19 20 21 4 22 23 24 25 26 27 28 5 29 30 31
如果這些命令都不適合您的需要,您可以使用
gcal
它來做您想做的事情。例子
$ gcal -K April 2014 Su Mo Tu We Th Fr Sa CW 1 2 3 4 5 13 6 7 8 9 10 11 12 14 13 14 15 16 17 18 19 15 20 21 22 23 24 25 26 16 27 28 29 30 17
在右側的最後一列中列印週數。
參考
這會突出顯示今天的日期,並且可以通過
$1
以下形式顯示任何月份:YYYY-mm-dd
… 預設為今天的日期它設置為顯示 ISO 週數,第一個工作日是星期一。
#!/bin/bash # Input reference date is expected in 'YYYY-mm-dd' format # today=($(date '+%Y %m %d')); Y=0; m=1; d=2 # establish today's date [[ -z $1 ]] && ref=(${today[@]}) || ref=(${1//-/ }) # get input date dNbA=$(date --date="$(date +%Y-%m-01)" +'%u') # day-number of 1st day of reference month today[m]=$((10#${today[m]})); ref[m]=$((10#${ref[m]})) # remove leading zero (octal clash) today[d]=$((10#${today[d]})); ref[d]=$((10#${ref[d]})) # remove leading zero (octal clash) nxtm=$(( ref[m]==12 ?1 :ref[m]+1 )) # month-number of next month nxtY=$(( ref[m]==12 ?ref[Y]+1:ref[Y] )) # year-number of next month nxtA="$nxtY-$nxtm-1" # date of 1st day of next month refZ=$(date --date "$(date +$nxtA) yesterday" +%Y-%m-%d) # date of last day of reference month days=$(date --date="$refZ" '+%d') # days in reference month h1="$(date --date="${ref[Y]}-${ref[m]}-${ref[d]}" '+%B %Y')" # header 1 h2="Mo Tu We Th Fr Sa Su" # header 2 printf " %$(((${#h2}-${#h1}-1)/2))s%s\n" " " "$h1" printf " %s\n" "$h2" # print week rows printf "%2d " "$((10#$(date -d "$(date +${ref[Y]}-${ref[m]}-01)" +'%V')))" # week-number (of year) with suppressed leading 0 printf "%$(((dNbA-1)*3))s" # lead spaces (before start of month) dNbW=$dNbA # day-number of week dNbM=1 # day-number of month while ((dNbM <= days)) ;do if (( today[Y]==ref[Y] && today[m]==ref[m] && today[d]==dNbM )) ;then printf "\x1b[7m%2d\x1b[0m " "$dNbM" # highlight today's date else printf "%2d " "$dNbM" fi ((dNbM++)) if ((dNbW >=7)) ;then cdate=$((10#$(date -d "$(date +${ref[Y]}-${ref[m]}-$dNbM)" +'%V'))) # remove leading zero (octal clash) printf "\n%2d " "$cdate" # week-number of year dNbW=0 fi ((dNbW++)) done printf "%$(((8-dNbW)*3))s\n" # trailing spaces (after end of month)
這是本月的顯示(
20
突出顯示)January 2012 Mo Tu We Th Fr Sa Su 52 1 1 2 3 4 5 6 7 8 2 9 10 11 12 13 14 15 3 16 17 18 19 20 21 22 4 23 24 25 26 27 28 29 5 30 31