Re: [問題] date 輸出每個月的日期?

看板Linux作者時間14年前 (2011/05/26 11:32), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/4 (看更多)
※ 引述《Xphenomenon.bbs@ptt.cc (啦 )》之銘言: > 請教一下,我知道 date 可以知道目前的時間日期以及前後天等這類的資訊, > 不知道它是否可以我給定它譬如: 2011年 2 月份,它會回傳給我 28 天; > 給他 2008 年 2 月,他會回傳給我 29 天。不知道是否可以實現?謝謝! 用以下指令可以搞定: cal `date '+%m'` `date '+%Y'` | grep . | fmt -1 | tail -1 若你想直接查詢某年某月 example: 查詢 2008年2月 cal 02 2008 | grep . | fmt -1 | tail -1 若寫成shell script的話: #!/bin/bash if [ "$1" = "" ] then YEAR=`date '+%Y'` else YEAR=$1 fi if [ "$2" = "" ] then MONTH=`date '+%m'` else MONTH=$2 fi DAY_IN_MONTH=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1` echo "$YEAR/$MONTH has $DAY_IN_MONTH days" 假設你將這檔案存成 aa.sh 使用方法: ./aa.sh 2008 02 會得到 2008/02 has 29 days -- 常常要對自己想,自己沒甚麼了不起,只是運氣好而已。 在往上爬的過程中,千萬不能忘了下面的人,一定要幫助他們。 --- 李家同 -- 東華電機‧後山星城 bbs.ee.ndhu.edu.tw ─ ├ 文章來源﹢ymu041-045.ym.edu.tw --- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
文章代碼(AID): #1DtScoXi (Linux)
文章代碼(AID): #1DtScoXi (Linux)