#写一个脚本判断给定的一串数字是否是合法的日期
##比如20231301就不合法
#!/bin/bash
#判断是否提供一个参数
#判断提供参数长度是否是8
if [ $# -ne 1 ] || [ ${#1} -ne 8 ]
then
echo "Usage: bash $0 yyyymmdd"
exit 1
fi
mydate=$1
##截取前4个字符
year=${mydate:0:4}
##截取第5个到第6个字符
month=${mydate:4:2}
## 截取第7个到第8个字符
day=${mydate:6:2}
if cal $day $month $year >/dev/null 2>/dev/null
then
echo "The date is OK. the date is $year年$month月$day日"
else
echo "The date is Error"
fi
<<"COMMENT"
1) $#表示参数个数,${#1}表示第一个参数的个数
2)${a:n1:n2}表示容量a从n1(从0开始计算)个字符开始截取,一共截取n2个
3)cal为linux的日志,用法:cal 日 月 年; cal 月 年; cal 年; cal
COMMENT