2013-05-07 52 views
2

我想在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
# other stuff 
elif ((`date +%s -d $tDate` >= `date +%s`)) 
then 
    echo 'Date must be in the past. Please try again.' 
    # other stuff 
+1

感謝您的幫助。 – Eric 2013-05-07 16:57:00