5 Replies Latest reply on Dec 11, 2017 6:36 PM by Dude!

    date comparison

    vikas yadav 250190-Oracle

      my requirement is to check the expiry date of a column with the parameter passed.

       

      I have a shell script that gets the data into a file. The data in file is below,

       

      pub, 2048R/60AEBB3A, 2016-04-11, 2018-06-16

      sub, 2048g/67AEFBFA, 2016-04-11, 2018-06-16

       

      I need to print the date in the below format in the log file,

      type     number          creation date  expiry date   expiring or not in $days

      -------- --------------- -------------- ------------  ------------------------------------

      pub 2048R/60AEBB3A 2016-04-11 2018-06-16    yes/no

      sub 2048g/67AEFBFA 2016-04-11 2018-06-16 yes/no

       

      $days will be passed as parameter from the concurrent program.

      the expiry date needs to compared with the sysdate+$days. if  it is within sysdate+$days 'yes' needs to be printed for that record, if not  then no.

       

      I am getting stuck doing this date comparison in the script.

       

      *note: no table has to be used.

        • 1. Re: date comparison
          Dude!

          I think one way to approach this is to convert the date to seconds since epoch.

           

          Then convert $days to seconds and the rest is simply math.

           

          For example:

           

          date -d 2017-04-11 +%s

          1491861600

           

          date -d 2017-04-12 +%s

          1491948000

           

          echo $((1491948000 - 1491861600))

          86400

           

          echo $((86400 / 3600 / 24))

          1

          • 2. Re: date comparison
            vikas yadav 250190-Oracle

            how can I handle this logic in my shell script??

             

             

            echo "`gpg --list-keys test at date | grep "\[expires:" | awk -O {' print $1,$2,$3,$5'}|sed -e 's/\]//g'`" > /dbro1i/applmgr/CUSTOM/xbol/12.0.0/bin/GPGKEYDETAILS.txt

            chmod 777 GPGKEYDETAILS.txt

            cat $FILE_NAME

            var4="$ `date '+%Y-%m-%d' -d '+'$PERIOD_DAYS' days'` "

            printf " The future date to be checked from today is : " $var4

            echo `date '+%Y-%m-%d' -d '+'$PERIOD_DAYS' days'`

            for line in ` cat $FILE_NAME `; do

            echo $line

            done

             

            $FILE_NAME is /dbro1i/applmgr/CUSTOM/xbol/12.0.0/bin/GPGKEYDETAILS.txt

             

            i need to handle this in the for loop, as i can have multiple records going forward in the file.

            handling this inside loop will require me to pull the expiry date (which is the 4th string in the line) and doing the comparison with the sysdate +$days passed from the concurrent program.

            but the problem in my loop is its not reading line by line but word by word.

            so the cat $FILE_NAME gives the output like

            pub

            2048R/60AEBB3A

            2016-04-11

            2018-06-16

            sub

            2048g/67AEFBFA

            2016-04-11

            2018-06-16

             

            now from this output i need to pick 4th and 8th record for comparison

            • 3. Re: date comparison
              Andris Perkons-Oracle

              Hi,

               

              instead of this:

               

              for line in ` cat $FILE_NAME `; do
              echo $line
              done
              

               

              try this:

               

              while read param1 param2 date1 date2; do
                echo $param1 $param2 $date1 $date2
              done < $FILE_NAME
              

               

              Andris

              • 4. Re: date comparison
                vikas yadav 250190-Oracle

                i have modified the code now, to

                 

                IFS=$'\n'

                for line in ` cat $FILE_NAME `; do

                var5=`echo $line|awk '{print $4}'`

                var6=`date -d '$var5' +%s`

                var4="$ `date '+%Y-%m-%d' -d '+'$PERIOD_DAYS' days'` "

                var7=`date -d '$var4' +%s `

                printf ''$var5' \n'

                if [$var6 -ge $var7]; then

                printf 'no\n'

                else

                printf 'yes\n'

                fi

                done

                 

                but finding it dfifficult to convert the date into seconds

                • 5. Re: date comparison
                  Dude!

                  How about the following:

                   

                  #!/bin/bash
                  infile=infile
                  outfile=outfile
                  fdate=$(date -d "now + $1 days" +%s)
                  while read -r line; do
                    xdate=$(cut -d, -f4 <<<$line)
                    xdate=$(date -d "$xdate" +%s)
                    [ $fdate -gt $xdate ] && ans=no || ans=yes
                    echo "$line, $ans" >>$outfile
                  done <$infile
                  

                   

                  $ cat infile
                  pub, 2048R/60AEBB3A, 2016-04-11, 2018-06-16
                  sub, 2048g/67AEFBFA, 2016-04-11, 2017-08-21
                  
                  $ ./testcmd 120
                  
                  $ cat outfile
                  pub, 2048R/60AEBB3A, 2016-04-11, 2018-06-16, yes
                  sub, 2048g/67AEFBFA, 2016-04-11, 2017-08-21, no
                  
                  1 person found this helpful