私は次の月の最初の日曜日、次の月の最初の月曜日、次の月の最初の火曜日、来月の最初の水曜日などを取得する単純なUnixコマンドを探しています。
完全な日付形式が必要です(時間は必須ではありません)。
2、3、4などの数字を取得できます。数字だけが必要ではないため、日付形式(日、月、年を含む)が必要です。
$ NEXT_MONTH=`date +'%m %Y' -d 'next month'` $ echo $NEXT_MONTH 04 2017 $ NEXT_SUNDAY=`cal $NEXT_MONTH | awk 'NF==7 && !/^Su/{print $1;exit}'` $ echo $NEXT_SUNDAY 2
メールグループに通知を送信するには、この日付が必要です。
たとえば、次のように来月の最初の土曜日を取得できます。
$ firstofmonth=$(date -d '+1 months' '+%Y%m01')
20170401
$ firstsaturday=$(date -d "$firstofmonth" '+%Y-%m')-$((7 - \
$(date -d "$firstofmonth" '+%u') ))
2017-04-1
ベストアンサー1
存在するksh93
:
$ printf '%(%F)T\n' 'next month, first Monday'
2017-04-03
bash
%(<format>)T
、4.2は現在buitinでaをサポートしていますprintf
が、そのような日付式を解析する機能はサポートしていません。
必ず使用する必要がbash
あり、使用したい場合は%(<format>)T
フォークしなくても使用できます。たとえば、次のようになります。
printf -v code '%(
t=$((%s + (12 - %-H) * 60 * 60))
increment=$((8 - %u))
current_month=%m)T' -1
eval "$code"
until
t=$((t + increment * 24 * 60 * 60)) # next Monday around mid-day
printf -v code '%(date=%F month=%m)T' "$t"
eval "$code"
[ "$month" != "$current_month" ] # until be get to next month
do
increment=7 # the first increment is the number of days
# til Monday. Next increments are just a week.
done
echo "$date"