2012. 7. 12. 17:27

#!/bin/bash

case "$2" in
    1|01)
        day=1
        endday=31;;
    2|02)
        day=32
        if [ `expr $1 % 4` -eq 0 ]; then
            if [ `expr $1 % 100` -ne 0 -o `expr $1 % 400` -eq 0 ]; then
                endday=29
            else
                endday=28
            fi
        else
            endday=28
        fi
        ;;
    3|03)
        day=60
        endday=31;;
    4|04)
        day=91
        endday=30;;
    5|05)
        day=121
        endday=31;;
    6|06)
        day=152
        endday=30;;
    7|07)
        day=182
        endday=31;;
    8|08)
        day=213
        endday=31;;
    9|09)
        day=244
        endday=30;;
    10)
        day=274
        endday=31;;
    11)
        day=305
        endday=30;;
    12)
        day=335
        endday=31;;
esac

# 2월이 윤달일 경우, 3월부터 $day 변수의 값을 1씩 증가시킨다.
if [ $2 -gt 2 ]; then
    if [ `expr $1 % 4` -eq 0 ]; then
        if [ `expr $1 % 100` -ne 0 -o `expr $1 % 400` -eq 0 ]; then
            day=`expr $day + 1`
        else
            day=$day
        fi
    else
        day=$day
    fi
fi

# AD 1년 1월 1일부터 지금까지 지난 일자 계산
year=`expr $1 - 1`
total_day=`expr \( $year \* 365 \) + \( $year / 4 \) - \( $year / 100 \) + \( $year / 400 \) + $day`
yoil=`expr $total_day % 7`

echo ""
echo "     $1년 $2월"
echo "===================="
echo "^[[31m일^[[0m 월 화 수 목 금 ^[[34m토^[[0m"
echo "===================="

i=1
while [ : ]
do
     for j in 1 2 3 4 5 6 7
     do
         A=`expr $i - $yoil`
         if [ "$A" -le 0 ]; then
             A=" "
         fi

         if [ "$A" -gt "$endday" ]; then
             if [ `expr $i % 7` -eq 1 ]; then
                 echo "===================="
                 echo ""
                 exit
             else
                 echo ""
                 echo "===================="
                 echo ""
                 exit
             fi
          fi

          if [ "$A" -lt 10 ]; then
              if [ $j -eq 1 ]; then
                 echo -n " ^[[31m$A^[[0m "
             elif [ $j -eq 7 ]; then
                echo -n " ^[[34m$A^[[0m "
            else
                echo -n " $A "
            fi
        else
            if [ $j -eq 1 ]; then
                echo -n "^[[31m$A^[[0m "
            elif [ $j -eq 7 ]; then
                echo -n "^[[34m$A^[[0m "
            else
                echo -n "$A "
            fi
        fi
    i=`expr $i + 1`
    done
    echo ""
done

Posted by 몰라욧