- shell判断闰年的星期
existDayInMonth(){
year="$1"
day="$3"
if [ "$year" -lt 1582 -o "$year" -gt 9999 ];then
echo -e "----------------------请输入1582-9999的年份!----------------------\n"
return 0
else
case $2 in
01|1|03|3|05|5|07|7|08|8|10|12)
if [ "$day" -lt 1 -o "$day" -gt 31 ];then
echo -e "----------------------$2月$3日是无效的日期----------------------\n"
return 0
else
return 1
fi
;;
02|2)
if [ "$day" -lt 1 -o "$day" -gt 28 ];then
echo -e "----------------------$2月$3日是无效的日期----------------------\n"
return 0
else
return 1
fi
;;
04|4|06|6|09|9|11)
if [ "$day" -lt 1 -o "$day" -gt 30 ];then
echo -e "----------------------$2月$3日是无效的日期----------------------\n"
return 0
else
return 1
fi
;;
*)
echo -e "----------------------日历表中没有$2这个月----------------------\n"
return 0
;;
esac
fi
}
#判断是否为闰年
isLeapYear(){
year="$1"
if [ "$((year % 4))" -eq 0 -a "$((year % 100))" -ne 0 ];then
echo -e "******$year年是闰年!"
elif [ "$((year % 400))" -eq 0 ];then
echo -e "******$year年是闰年!"
else
echo -e "******$year年是平年!"
fi
}
#判断日期是星期几-蔡勒公式
whatDay(){
yearstr="$1"
c=${yearstr:0:2} #年份前两位数
y=${yearstr:0-2:2} #年份后两位数
m="$2" #月(m的取值范围为3至14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算)
d="$3" #日
case $2 in
01|1|02|2)
y=`expr $y - 1`
m=`expr $m + 12`
;;
*)
;;
esac
#计算W所得的数值对应的星期:0-星期日;1-星期一;2-星期二;3-星期三;4-星期四;5-星期五;6-星期六
week=$(($((26*$((m +1))))/10))
wl=`expr $y + $y / 4 + $c / 4 - 2 \* $c + $week + $d - 1`
if [ "$wl" -ge 0 ]
then
W=$((wl % 7))
else
W=$((wl % 7 + 7))
fi
case $W in
0) echo -e "******$year年$m月$d日是星期日!!" ;; 1) echo -e "******$year年$m月$d日是星期一!!" ;;
2) echo -e "******$year年$m月$d日是星期二!!" ;; 3) echo -e "******$year年$m月$d日是星期三!!" ;;
4) echo -e "******$year年$m月$d日是星期四!!" ;; 5) echo -e "******$year年$m月$d日是星期五!!" ;;
6) echo -e "******$year年$m月$d日是星期六!!" ;; *) ;;
esac
}
while true
do
if read -t 30 -p "**年份为四位数(1582以后)**输入年月日(例如1582.10.15):" dates
then
str="$dates";
arr=(${str//./ }); #将输入的日期进行分割
echo -e "\n您输入的是${arr[0]}年${arr[1]}月${arr[2]}日,判断结果如下:"
existDayInMonth ${arr[0]} ${arr[1]} ${arr[2]}
if [ $? -eq 1 ];then
isLeapYear ${arr[0]}
whatDay ${arr[0]} ${arr[1]} ${arr[2]}
fi
else
echo -e "\n30s未输入已自动退出"
exit 1
fi
done
- awk格式化输出
文件:成绩表,score.txt
lh 92 68 70
zyt 94 88 75
ls 96 78 78
hgw 90 70 56
yxx 80 88 73
hz 90 98 70
xyd 60 88 73
hj 90 58 70
cs 50 58 11
脚本 cal.awk
#!/bin/awk -f
#运行前
BEGIN {
math = 0
english = 0
chinese = 0
a = 0
b = 0
c = 0
d = 0
e = 0
printf "NAME YUWEN MATH ENGLISH TOTAL AVG\n"
printf "---------------------------------------------\n"
}
#运行中
{
math+=$3
english+=$4
chinese+=$2
printf "%-6s %4d %8d %8d %8d %8.2f\n", $1, $2, $3,$4, $3+$4+$2,($3+$4+$2)/3
avg=($3+$4+$2)/3
if(avg < 60)
a++
if(avg > 60 && avg <70)
b++
if(avg > 70 && avg <80)
c++
if(avg > 80 && avg <90)
d++
if(avg > 90 && avg <100)
e++
}
#运行后
END {
printf "---------------------------------------------\n"
printf " TOTAL:%10d %8d %8d \n", math, english, chinese
printf "AVERAGE:%10.2f %8.2f %8.2f\n", math/NR, english/NR, chinese/NR
}
执行结果:./cal.awk score.txt
NAME YUWEN MATH ENGLISH TOTAL AVG
---------------------------------------------
lh 92 68 70 230 76.67
zyt 94 88 75 257 85.67
ls 96 78 78 252 84.00
hgw 90 70 56 216 72.00
yxx 80 88 73 241 80.33
hz 90 98 70 258 86.00
xyd 60 88 73 221 73.67
hj 90 58 70 218 72.67
cs 50 58 11 119 39.67
---------------------------------------------
TOTAL: 694 576 742
AVERAGE: 77.11 64.00 82.44
推荐阅读:https://blog.csdn.net/qq_33283716/article/details/80865526