怎么判断我输入的数字长度??

read -p "Please input the date you want ,ex>20000120:" date
我怎么来判断我输入的日期是按格式输入,输入的是8位数字????

作者: space01   发布时间: 2011-08-13

[root@localhost ~]# cat date.sh
#/bin/bash
read -p "please import the date you want :ex>20000120 " date
e=`echo $date | awk '{if($0>20000120) print $0}' | grep "^2[0-9]\{3\}[01][0-9][0-3][0-9]$" `
if [ $? -eq 0 ] ;then
time=`echo $e | sed 's/\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\1 \2 \3/'`
a=`echo $time | awk '{ print $1}'`
b=`echo $time | awk '{ print $2 "\t" $3}' | tr ' 0' ' '`
echo $a $b | awk '{if ($1>=2000&&$2>0&&$2<=12&&$3>0&&$3<=31) print "import the date format correct" ; else print "date format woring"}'
else
echo "date format woring..."
fi
[root@localhost ~]# ./date.sh
please import the date you want :ex>20000120 20000119  
date format woring...
[root@localhost ~]# ./date.sh
please import the date you want :ex>20000120 20000121
import the date format correct
[root@localhost ~]# ./date.sh
please import the date you want :ex>20000120 20001334
date format woring
[root@localhost ~]# ./date.sh
please import the date you want :ex>20000120 20101230
import the date format correct
可以判断输入的日期是日期格式正确的一个大于2000年01月20日的8位数字

作者: 风舞紫晓   发布时间: 2011-08-13

d=8位数字; [ "X$d" == "X$(date -d $d +%Y%m%d 2>/dev/null)" ] && echo 合法日期 || echo 非法日期
你自己测试一下吧

[ 本帖最后由 wenmin88888 于 2011-8-13 17:37 编辑 ]

作者: wenmin88888   发布时间: 2011-08-13

上面那个只能测试时间输入的对与错。控制字符还的加一个判断。

作者: wenmin88888   发布时间: 2011-08-13