验证日期格式在shell脚本 [英] Validate Date Format in a Shell Script

查看:202
本文介绍了验证日期格式在shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个shell脚本,其中的参数之一将是格式DD / MM / YYYY的日期。我的问题是,我怎么能检查是否作为参数传递的日期确实遵循此日期格式?我试图用如下grep命令:

I have to create a Shell Script wherein one of the parameters will be the date in the format dd/mm/yyyy. My question is, how can I check if the Date passed as parameter really follows this Date Format? I tried to use the grep command as below:

if echo "$1" | grep -q '^[0-3][0-9]/[0-1][0-9]/[0-9]\{4\}$'

但它没有给出正确的格式,因为例如一天可以33,34,(...),这是不是真的正确的格式。任何人都知道的东西,真的可以检查是否真的获得通过的日期的格式为DD / MM / YYYY?

but it didn't give the correct format because the day for example can be 33, 34, (...), that is not really the correct format. Anyone know something that can really check if the date passed really follows the format dd/mm/yyyy ?

推荐答案

使用的日期

date "+%d/%m/%Y" -d "09/99/2013" > /dev/null  2>&1
 is_valid=$?

如果你没有得到0,那么日期的格式无效。

If you do not get 0 then date is in invalid format.

这篇关于验证日期格式在shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆