shell脚本的万年历实现

./project2.sh 然后输入年和月,查询某年某月的万年历
./project2.sh -y 然后输入年和月,上下波浪形输出全年万年历,不算真正的万年历,属半成品
./project2.sh -m 然后输入年和月,上下波浪形输出某年某-m月到某年某+m月的万年历,仍是半成品
./project2.sh alert 然后输入年和月,完整显示全年的万年历
属完成作品
大体思路
显示某年某月万年历,是用函数oneday算出某月一号星期几number,函数fday算出某月的总天数,讲这两个参数代入函数display显示某月万年历
全年万年历是织布理论的很好体现。一行一个显示函数,先后调用display1,display2等函数,到第5行时再用display5判断要不要继续织下去。
$1 和$2在调用display3时使用了,不然最后的代码可以做成循环。

summ=0
sumy=0
sum=0
i=0
j=0
day=0
number1=0
printf "year:\n"
read year
printf "month:\n"
read month


function fday()
{
if [ $month -eq 1 -o $month -eq 3 -o $month -eq 5 -o $month -eq 7 -o $month -eq 8 -o $month -eq 10 -o $month -eq 12 ]
then
day1=31
elif [ $month -eq 4 -o $month -eq 6 -o $month -eq 9 -o $month -eq 11 ]
then 
day1=30
elif [ $month -eq 2 ]
then
    if  [[ $((year%4)) -eq 0 && $((year%100)) != 0 ]] || [ $((year%400)) == 0 ]
    then
    day1=29

    else
    day1=28
    fi
fi
echo "$day1"
}



function oneday()
{
sumy=0
summ=0
i=1990
while [ $i -le $((year-1)) ]
do
 let sumy+=1
#  if [ $((sumy%7)) -eq 0 ]
 #  then
  # let sumy=0
 #  fi
   if [[ $((i%4)) -eq 0 && $((i%100)) != 0 ]]||[ $((i%400)) -eq 0 ]
   then
   let sumy+=1
   fi
   #if [ $((sumy%7)) -eq 0 ]
 #   then
 #   let sumy=0
   # fi
    let i++
done
#summ=$((-1+2*month+3*(month+8)/5))
case $month in
1)
  summ=0
;;
2)
  summ=3
;;
3)
  summ=3
;;
4)
  summ=6
;;
5)
  summ=9
;;
6)
  summ=11
;;
7)
  summ=14
;;
8)
  summ=17
;;
9)
  summ=19
;;
10)
  summ=21
;;
11)
   summ=24
;;
12)
   summ=26
;;
esac

if [ $month -ge 3 ]
then
  if [[ $((i%4)) -eq 0 && $((i%100)) != 0 ]]||[ $((i%400)) -eq 0 ]
    then
    let summ+=1
    fi
fi
let sum=summ+sumy

let number1=(sum+1)%7
#if [ $number1 -eq 7 ]
# then
# let number1=0
# fi
 echo " $number1"
 }



function display()
{
 let j=0
 count=0
 let i=0
 number5=$((7*$1))
 while [ $i -le $number5 ]
 do 
 printf "   "
 let i++
 done
 printf "  %4dyear %2dmonth\n" "$year" "$month"
 let i=0
 while [ $i -le $number5 ]
 do 
 printf "   "
 let i++
 done
printf "ri yi er san si wu liu\n"
let i=0
while [ $i -le $number5 ]
do 
printf "   "
let i++
done
while [ $j -lt $number ]
do
  printf "   "
  
  let j++
  let count++
done

let j=1
while [ $j -le $day ]
do
  printf "%2d " "$j"
  if [ $(((j+number)%7)) -eq 0 ]
  then
  printf "\n"
  let i=0
  while [ $i -le $number5 ]
  do
  printf "   "
  let i++
  done
  fi
let j++
done

}





if [ $# -eq 0 ]
then
day=`fday`
printf "$day"
number=`oneday`
display 0

elif [ $1 = "-y" ]
then
let month=1
while [ $month -le 12 ]
do
songshiqi=$((month%4))
day=`fday`
number=`oneday`
display $songshiqi
printf "\n"
let month++
done

elif [ $1 = "-m" ]
then
min=$((month-$2))
max=$((month+$2))
if [ $min -ge 1 ]&&[ $max -le 12 ]
then
month=$min
while [ $month -le $max ]
do
songshiqi=$((month%4))
day=`fday`
number=`oneday`
display $songshiqi
printf "\n"
let month++
done
else
echo -e "超出当前程序处理的范围,请合理使用本程序\n"
fi
elif [ $1 = "-f" ]
then
echo -e "参数错误,参考参数为-y,-m\n"

elif [ $1 = "alert" ]
then
function display1()
{
let j=0
 count=0
 let i=0

 printf "   %4dyear %2dmonth   " "$year" "$month"
 }

 function display2()
 {

printf "日 一 二 三 四 五 六 "
}

function display3()
{

let j=0

while [ $j -lt $2 ]
do
  printf "   "
  
  let j++
  let count++
done

let j=1


while [ $(((j+$2)%7)) -ne 0 ]
do

  printf "%2d " "$j"
let  j++
done
 printf "%2d " "$j"
let array[p-1]=j


}


function display4()
{
 
let j=array[p-1]+1
#printf "$j"
while [ $(((j+number[p-1])%7)) -ne 0 ]
do
printf "%2d " "$j"
let j++
done
printf "%2d " "$j"
let array[p-1]=j

}
function display5()
{
let j=array[p-1]+1
if [ $j -gt ${day[$((p-1))]} ]
then
h=0
while [ $h -lt 7 ]
do
printf "   "
let h++
done
elif [ $j -le ${day[$((p-1))]} ]&&[ $((j+6)) -gt ${day[$((p-1))]} ]
then
h=$((j+6))
while [ $j -le ${day[$((p-1))]} ]
do
printf "%2d " "$j"
let j++
done
while [ $(((j+number[p-1])%7)) -ne 0 ]
do
printf "   "
let j++
done
printf "   "
let array[p-1]=day[$((p-1))]
else
while [ $(((j+number[p-1])%7)) -ne 0 ]
do
printf "%2d " "$j"
let j++
done
printf "%2d " "$j"
let array[p-1]=j
fi
}
arrayOfMonth1=(1 2 3 4)
arrayOfMonth2=(5 6 7 8)
arrayOfMonth3=(9 10 11 12)
number=(0 0 0 0)
array=(0 0 0 0)
day=(0 0 0 0)
p=0

function circle()
{
p=0
for p in ${arrayOfMonth1[*]}
do
let month=p
$1
if [ $p -eq 4 ]
then
printf "\n"
fi
done
}
for p in 1 2 3 4
do
let month=p
day[$((p-1))]=`fday`
number[$((p-1))]=`oneday`
display1

if [ $p -eq 4 ]
then
printf "\n"
fi
done

p=0
for p in 1 2 3 4
do
display2
if [ $p -eq 4 ]
then
printf "\n"
fi
done

p=0
for p in 1 2 3 4 
do
let month=p
display3 ${day[$((p-1))]} ${number[$((p-1))]}
if [ $p -eq 4 ]
then
printf "\n"
fi
done

circle display4
circle display4
circle display4
circle display5
circle display5

function circle1()
{
p=0
for p in 5 6 7 8
do
let month=p
$1
if [ $p -eq 8 ]
then
printf "\n"
fi
done
}
for p in 5 6 7 8
do
let month=p
day[$((p-1))]=`fday`
number[$((p-1))]=`oneday`
display1

if [ $p -eq 8 ]
then
printf "\n"
fi
done

p=0
for p in 5 6 7 8
do
display2
if [ $p -eq 8 ]
then
printf "\n"
fi
done

p=0
for p in 5 6 7 8
do
let month=p
display3 ${day[$((p-1))]} ${number[$((p-1))]}
if [ $p -eq 8 ]
then
printf "\n"
fi
done

circle1 display4
circle1 display4
circle1 display4
circle1 display5
circle1 display5

function circle2()
{
p=0
for p in 9 10 11 12
do
let month=p
$1
if [ $p -eq 12 ]
then
printf "\n"
fi
done
}
for p in 9 10 11 12
do
let month=p
day[$((p-1))]=`fday`
number[$((p-1))]=`oneday`
display1

if [ $p -eq 12 ]
then
printf "\n"
fi
done

p=0
for p in 9 10 11 12
do
display2
if [ $p -eq 12 ]
then
printf "\n"
fi
done

p=0
for p in 9 10 11 12
do
let month=p
display3 ${day[$((p-1))]} ${number[$((p-1))]}
if [ $p -eq 12 ]
then
printf "\n"
fi
done

circle2 display4
circle2 display4
circle2 display4
circle2 display5
circle2 display5

fi

你可能感兴趣的:(shell脚本的万年历实现)