#!/bin/bash #出力先を指定 txt=quiz2.txt #年・月の順に入力し出力先に書き込む echo 'Enter your YEAR and MONTH.' read year month echo $year $month >> $txt printf "%2s %2s %2s %2s %2s %2s %2s\n" "Su" "Mo" "Tu" "We" "Th" "Fr" "Sa" | tee -a $txt #各月の最終日を決定する if [ $month = 2 ] then if [ $(($year % 4)) = 0 ] then last=29 else last=28; fi if [ $(($year % 100)) = 0 -a $(($year % 400)) != 0 ] then last=28; fi elif [ $month = 4 -o $month = 6 -o $month = 9 -o $month = 11 ] then last=30 else last=31; fi #Zellerの公式より、まず1・2月を前年の13・14月に置き換える if [ $month = 1 -o $month = 2 ] then y=`expr $year - 1` m=`expr $month + 12` else y=$year m=$month; fi #Zellerの公式を適用し1日から最終日までの曜日を決定する d=1 while [ $d -le $last ]; do h=`expr $(($y + $(($y / 4)) - $(($y / 100)) + $(($y / 400)) + $(($(($(($m * 13)) + 8)) / 5)) + $d)) % 7` #日曜から順に1週間分の日にちを決めていく while [ $h -le 6 ]; do case $h in 0) if [ $d -le $last ]; then Su=$d ; else Su=' '; fi ;; 1) if [ $d -le $last ]; then Mo=$d ; else Mo=' '; fi ;; 2) if [ $d -le $last ]; then Tu=$d ; else Tu=' '; fi ;; 3) if [ $d -le $last ]; then We=$d ; else We=' '; fi ;; 4) if [ $d -le $last ]; then Th=$d ; else Th=' '; fi ;; 5) if [ $d -le $last ]; then Fr=$d ; else Fr=' '; fi ;; 6) if [ $d -le $last ]; then Sa=$d ; else Sa=' '; fi ;; esac h=`expr $h + 1` d=`expr $d + 1` done #土曜が決まると内側のwhileを抜け1週間分の日にちを出力し、次の週に移行 printf "%2s %2s %2s %2s %2s %2s %2s\n" "$Su" "$Mo" "$Tu" "$We" "$Th" "$Fr" "$Sa" | tee -a $txt done #最終週が出力されると外側のwhileを抜け終了