怎么判断我输入的数字长度??
时间:2011-08-13
来源:互联网
read -p "Please input the date you want ,ex>20000120:" date
我怎么来判断我输入的日期是按格式输入,输入的是8位数字????
我怎么来判断我输入的日期是按格式输入,输入的是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位数字
#/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-8-13 17:37 编辑 ]
作者: wenmin88888 发布时间: 2011-08-13
上面那个只能测试时间输入的对与错。控制字符还的加一个判断。
作者: wenmin88888 发布时间: 2011-08-13
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28