巧妙的检查日期方法[@more@]
#! /bin/bash
isdate()
{
cal $2 $1 2>/dev/null | grep -wq "${3#0}" && return 0 || return 1
}
line=$(head -1 urfile)
linenum=${line:35:4}
realnum=$(($(wc -l < urfile)-1))
isdate ${line:2:4} ${line:6:2} ${line:8:2} || { echo "first date is wrong !"; exit 1; }
isdate ${line:14:4} ${line:18:2} ${line:20:2} || { echo "second date is wrong !"; exit 1; }
[ $linenum -eq $realnum ] 2>/dev/null || { echo "line num is wrong !"; exit 1; }
echo "file check OK !"
urfile:
102008042400002008042400000600636000015
1013392034
3016883862
1013287794
3016882390
1013538866
3016884294
0220542482
3016883846
1016482930
0220514994
这个方法使用系统月历cal,很巧妙.但有一个问题,到year中包含字母,如200u就会得出错误,还需要加一个判断:输入变量都为数字.
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/640706/viewspace-1043916/
,如需转载,请注明出处,否则将追究法律责任。