Using date operations and comparisons in the shell

Source: Internet
Author: User
Tags diff

#!/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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.