LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Unable to subtract current date and future date in YYYY-MM-DD format (https://www.linuxquestions.org/questions/linux-newbie-8/unable-to-subtract-current-date-and-future-date-in-yyyy-mm-dd-format-4175629396/)

archie7 05-10-2018 07:16 AM

Unable to subtract current date and future date in YYYY-MM-DD format
 
I want to write a script to calculate the difference between the current date and expiry date of a certificate and if the difference is less than 30 days then generate some kind of notification, for example, today is 2018-05-10 and certificate expiry date is 2018-05-30?

syg00 05-10-2018 07:21 AM

So show us what you have done to make it happen.

pan64 05-10-2018 07:22 AM

which language do you prefer at all?

Ginola 05-10-2018 07:34 AM

Code:

#!/bin/bash

TODAY_DATE_30=`date -d 'now + 30 days' +%s`
CERT_DATE=`date -d "$1" +%s`

if [ "$TODAY_DATE_30" -ge "$CERT_DATE" ]
then
echo "less that 30 days!!"
fi


Code:

$ ./plus_30.sh 2018-06-10
$ ./plus_30.sh 2018-06-09
less that 30 days!!
$


rtmistler 05-10-2018 08:13 AM

Quote:

Originally Posted by archie7 (Post 5852797)
I want to write a script to calculate the difference between the current date and expiry date of a certificate and if the difference is less than 30 days then generate some kind of notification, for example, today is 2018-05-10 and certificate expiry date is 2018-05-30?

Please review the forum guidelines about how to ask effective questions.

As of late posts like this have been being closed due to a poor question. Especially when they are first posts from new users.

While this is your first post, you've been an LQ member for a few months and likely viewing questions a lot.

I will leave this question open to allow you to respond to the members with any feedback.

Please provide updates to the users who have asked for feedback as well as one who has provided some great guidance in the form of a bash script.

archie7 05-11-2018 12:23 AM

Quote:

Originally Posted by Ginola (Post 5852806)
Code:

#!/bin/bash

TODAY_DATE_30=`date -d 'now + 30 days' +%s`
CERT_DATE=`date -d "$1" +%s`

if [ "$TODAY_DATE_30" -ge "$CERT_DATE" ]
then
echo "less that 30 days!!"
fi


Code:

$ ./plus_30.sh 2018-06-10
$ ./plus_30.sh 2018-06-09
less that 30 days!!
$



Thanks, Ginola for the help. I will try this script to see if this works according to my requirement. But since I have just started working with shell scripts I do not know how do I use below lines in my code:

Code:

$ ./plus_30.sh 2018-06-10
$ ./plus_30.sh 2018-06-09
less than 30 days!!
$

[/QUOTE]

Can you please help regarding this?

archie7 05-11-2018 12:34 AM

Quote:

Originally Posted by pan64 (Post 5852799)
which language do you prefer at all?

Bash script

pan64 05-11-2018 12:39 AM

Quote:

Originally Posted by archie7 (Post 5853106)
I do not know how do I use below lines in my code

We still have not seen your code, so probably in line 5.

archie7 05-11-2018 01:42 AM

Quote:

Originally Posted by pan64 (Post 5853111)
We still have not seen your code, so probably in line 5.

Hi pan, I was actually talking about the code posted by ginola.

archie7 05-11-2018 01:47 AM

Hi all,

I found a command which helped me:

echo $((($(date -u -d certificate expiry date in yyyy-mm-dd format[/B] +%s) - $(date -u -d current date in yyyy-mm-dd format +%s)) / 86400)).

The output of this command gives the difference between two dates in terms of days.

Ginola 05-11-2018 03:09 AM

Quote:

Originally Posted by archie7 (Post 5853123)
Hi all,

I found a command which helped me:

echo $((($(date -u -d certificate expiry date in yyyy-mm-dd format[/B] +%s) - $(date -u -d current date in yyyy-mm-dd format +%s)) / 86400)).

The output of this command gives the difference between two dates in terms of days.

You will still need to put that command in a shell script to accomplish you goal in the OP.


check this out...

https://www.shellscript.sh/

HTH.

archie7 05-11-2018 03:29 AM

Quote:

Originally Posted by Ginola (Post 5853152)
You will still need to put that command in a shell script to accomplish you goal in the OP.


check this out...

https://www.shellscript.sh/

HTH.

Yes, I have tested the command by putting it in the bash script and its working fine.


All times are GMT -5. The time now is 06:00 PM.