使用AWK在shell中生成日歷小程序
更新時間:2019年07月31日 14:45:30 作者:2lovecode
這篇文章主要為大家詳細介紹了如何使用AWK在shell中生成日歷小程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
最近在學習sed和awk時,發(fā)現(xiàn)了一本入門級別的好書:《Software Design 中文版 03》。
我們這里的日歷程序也是從那里得來,這里強烈推薦希望了解sed和awk的同志們?nèi)胧直緯?/p>
代碼段:
# 在shell中生成日歷 # 使用方式 # awk -f thisFile +2017 # awk -f thisFile +2017.10 # awk -f thisFile +2017.10.10 # function whichWeek(y, m, d, s) { s = 0 if (((y%4 == 0) && (y%100 != 0)) || (y%400 == 0)) if (M[2] == 28) M[2]++ if (y>0) s += --y + int(y/4) - int(y/100) + int(y/400) while (m > 1) s += M[--m] return (s+d)%7 } function printCalendar(y, m, d, nowWeek, i){ printf("%d%s %d%s\n", y, "年", m, "月") print "日 一 二 三 四 五 六 " printf("%s", repeat(" ", ((nowWeek+1)%7)*3)) for (i=1; i<=M[m]; i++) { if ((nowWeek+i) % 7 != 6) { printf("%2d ", i) } else { printf("%2d \n", i) } } print "\n" } function repeat(flag, time, i, str){ for (i=0; i<time; i++) { str = str flag } return str } function option(opt) { if (ARGC > 1 && ARGV[1] ~ /^\+.*$/) { print ARGC, ARGV[1] opt = substr(ARGV[1], 2) delete ARGV[1] } return opt } BEGIN { M[1] = 31 M[2] = 28 M[3] = 31 M[4] = 30 M[5] = 31 M[6] = 30 M[7] = 31 M[8] = 31 M[9] = 30 M[10] = 31 M[11] = 30 M[12] = 31 W[0] = "日" W[1] = "一" W[2] = "二" W[3] = "三" W[4] = "四" W[5] = "五" W[6] = "六" split(option(), date, ".") nowWeek = whichWeek(date[1]+0, date[2]+0, date[3]+0) if (date[3] != "") { print "星期" W[nowWeek] } else if (date[2] != "") { printCalendar(date[1]+0, date[2]+0, M[date[2]], nowWeek) } else { for (j=1; j<=12; j++) { printCalendar(date[1]+0, j, M[i], whichWeek(date[1]+0, j, 0)) } } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
shell腳本中執(zhí)行python腳本并接收其返回值的例子
這篇文章主要介紹了shell腳本中執(zhí)行python腳本并接收其返回值的例子,本文重點在于如何接收python腳本的返回值,需要的朋友可以參考下2014-08-08jq命令對JSON進行過濾遍歷結(jié)構(gòu)轉(zhuǎn)換等操作實例
這篇文章主要介紹了jq命令對JSON進行過濾遍歷結(jié)構(gòu)轉(zhuǎn)換等操作實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01shell腳本ssh遠程執(zhí)行命令給變量賦值的問題解決
本文主要介紹了shell腳本ssh遠程執(zhí)行命令給變量賦值的問題解決,就是從A機器通過SSH方式到B機器,并執(zhí)行相關(guān)的命令,具有一定的參考價值,感興趣的可以了解一下2023-07-07Idea使用Bashsupport插件編輯Shell腳本的方法
這篇文章主要介紹了Idea插件之——Bashsupport編輯Shell腳本的相關(guān)知識,功能非常齊全有檢查錯誤并且還可以在idea中直接運行shell腳本,本教程帶領(lǐng)大家一步步演示插件的安裝和配置,需要的朋友可以參考下2021-05-05