在Unix中验证日期

时间:2014-02-18 14:37:36

标签: date unix

我有一个场景如下:

我将得到两个日期即。以yyyyMMdd格式的start_dt和end_dt作为我的Unix脚本的参数。

我必须循环遍历从start_dt开始直到每次递增一天的end_dt的每个日期。

我写的脚本如下:

start_date=$1
end_date=$2

#verify dates
if ! date -d "$start_date" 2>&1 > /dev/null ; 
then echo "start date is invalid" ; exit 1
fi

if ! date -d "$end_date" 2>&1 > /dev/null ; 
then echo "end date is invalid" ; exit 1
fi

#set current and end date
curr_dt=$(date -d "$start_date")
end_dt=$(date -d "$end_date +1 hours")

#loop over all dates
while [ "$end_dt" != "$curr_dt" ] 
do
   echo $curr_dt        

         # increment the date
   curr_dt=$(date -d "$curr_dt +1 hours")
done

但是,当我使用20140128和20140130的输入参数运行时,我遇到了以下错误:

date: invalid date `20140130 +1 hours'
Tue Jan 28 00:00:00 EST 2014

date: invalid date `Tue Jan 28 00:00:00 EST 2014 +1 hours'

0 个答案:

没有答案