#!/bin/bashstartdate= "$" enddate= "$" date_fmt= "%y%m%d" source ./log.shexport debug= "true" function showusage () { warn "warn "------------------------ Usage ---------------- ------------- "warn " Syntax: checkInputDates.sh <start date> <end date> "warn " "warn " Remark: " warn " 1.two arguments are required and must with format ' YYYYmmDD '. " warn " 2.Named ' end date ' argument must be greater then or equal to ' Start date ' argument. warn "" warn " example: checkinputdates.sh 20140102 20140901" warn "------------------------------------------------------------ "}function checkdatevalid () {local mydate=$1local exp= "date + $DATE _fmt -d $myDate"if [ " $myDate " == " $exp "" ]; thenecho "1" else echo "0" Fi } function datesdiff () {local d1= ' date + $DATE _fmt -d $1 ' local d2= ' date + $DATE _fmt -d $2 ' local cr=$ (($d 1-$d 2)); # #Diff in ' Day ' unit.echo $cr}function comparedates () { local cr=$ (Datesdiff $1 $2) # #Diff in ' Day ' unit.# #debug "compare: $ (($ (date + $DATE _ fmt -d $1) - $ (date + $DATE _fmt -d $2)); "; if [[ $CR -eq 0 ]]; then echo "0" elif [[ $CR -gt 0 ]]; thenecho "1" else echo "1" fi}function totomorrow () {local curdate=$1# #debug ' date -d ' $curDate +1 day ' + $DATE _fmt ' # #echo ' date -d ' $curDate +1 day&nBSP; " + $DATE _fmt ' echo $ ((' date + $DATE _fmt -d $curDate ' + 1))}###### check inputs ################ #1 . only two arguments.warn "" If [ $# != 2 ]; thenwarn "Error: only support two arguments input." | showUsage exit 1;fi #2. check if they ' re the leagal value.tmpsd=$ (checkdatevalid $startDate) if [ "$tmpSd" == "0" ]; then ## #It ' s the invalid date. eg: 20141901warn ' ERROR: Invalid ' Start date ' with input ' $startDate ', it must be with format ' YyyyMMdd ', eg: 20140101 " | showusageexit 1;fi tmped=$ ( checkdatevalid $endDate) if [ "$tmpEd" == "0" ]; then ## #It ' s the invalid date. eg: 20141901 warn "error: invalid ' end date ' with input ' $startDate ', it must be with format ' YyyyMMdd ', eg: 20140101 " exit 1;fi# 3. check the arg2 greater than or equal arg1.# #echo "Compare result: "$ (comparedates $endDate $startDate) cprs=$ (comparedates $endDate $ StartDate) echo "diff days: $ (datesdiff $endDate $startDate)." if [ "$cprs" == "-1" ]; thenwarn "error: the first argument must not greater than the second one! " | showusage exit 1;fi####################################### #loop the dates between start and end date.while [[ "$startDate" != "$endDate" ]]doecho $startDatestartDate =$ (totomorrow $startDate) done echo " ---input arguments---"echo " startdate: "$startDateecho ---input arguments---"
Using date operations and comparisons in the shell