测试日期格式以确保输入正确

时间:2013-05-07 15:34:09

标签: bash

我正在尝试用bash编写测试,检查是否已正确输入日期(或者根本输入了日期)。这是我正在尝试做的事情:

tDate=$(lastCOB)
tDateOkay=0

until [ $tDateOkay -eq 1 ] ; do
    read -p "Please Enter date for search.  Use format: Date (YYYYMMDD): " -e -i "$tdate" tDate
        if [[ -z "$tDate" || {check for valid YYMMDD format}]] ; then
                echo "Invalid date. Please enter date in the correct format."
        elif [[ $tDate -gt $(today)|| $tdate -eq $(today) ]] ; then
                echo "Date must be in the past.  Please try again."
        else
            tDateOkay=1
        fi
done

日期必须是过去的日期,必须以正确的格式写入,否则数据将不会从正确的文件夹中提取。谢谢。

1 个答案:

答案 0 :(得分:1)

# other stuff
elif (( `date +%s -d $tDate` >= `date +%s` ))
then
  echo 'Date must be in the past.  Please try again.'
  # other stuff