例49
需求:用户输入一个日期,判断是否合法
#!/bin/bash
if [ $# -ne 1 ] || [ ${#1} -ne 8 ]
then
echo "请输入正确的格式,sh $0 yyyymmdd"
exit 1
fi
y=`echo ${1:0:4}`
m=`echo ${1:4:2}`
d=`echo ${1:6:2}`
if echo $d| grep -q "^0"
then
d=`echo ${1:6:2}|sed 's/^0//'`
fi
if cal $m $y >/dev/null 2>/dev/null
then
if ! cal $m $y|grep -qw "$d"
then
echo "你给的日期是不合法的"
else
echo "日期合法"
fi
else
echo "日期不合法"
fi
知识点:
[root@ts198 datif]# b=123456
[root@ts198 datif]# echo ${b:0:2}
12