LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-10-2018, 07:16 AM   #1
archie7
LQ Newbie
 
Registered: Nov 2017
Posts: 7

Rep: Reputation: Disabled
Post 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?
 
Old 05-10-2018, 07:21 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,145

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
So show us what you have done to make it happen.
 
Old 05-10-2018, 07:22 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,976

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
which language do you prefer at all?
 
Old 05-10-2018, 07:34 AM   #4
Ginola
Member
 
Registered: Sep 2012
Location: London
Distribution: CentOS, RHEL, Ubuntu
Posts: 73

Rep: Reputation: Disabled
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!!
$
 
Old 05-10-2018, 08:13 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Quote:
Originally Posted by archie7 View Post
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.
 
Old 05-11-2018, 12:23 AM   #6
archie7
LQ Newbie
 
Registered: Nov 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Ginola View Post
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?

Last edited by archie7; 05-11-2018 at 12:33 AM.
 
Old 05-11-2018, 12:34 AM   #7
archie7
LQ Newbie
 
Registered: Nov 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
which language do you prefer at all?
Bash script
 
Old 05-11-2018, 12:39 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,976

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
Quote:
Originally Posted by archie7 View Post
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.
 
Old 05-11-2018, 01:42 AM   #9
archie7
LQ Newbie
 
Registered: Nov 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
We still have not seen your code, so probably in line 5.
Hi pan, I was actually talking about the code posted by ginola.
 
Old 05-11-2018, 01:47 AM   #10
archie7
LQ Newbie
 
Registered: Nov 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-11-2018, 03:09 AM   #11
Ginola
Member
 
Registered: Sep 2012
Location: London
Distribution: CentOS, RHEL, Ubuntu
Posts: 73

Rep: Reputation: Disabled
Quote:
Originally Posted by archie7 View Post
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.
 
Old 05-11-2018, 03:29 AM   #12
archie7
LQ Newbie
 
Registered: Nov 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Ginola View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
midnight commander date format setting YYYY:MM:DD HH:MM:SS Mario Blunk Linux - Desktop 2 07-07-2015 10:23 AM
create file with Julian date and current time --- then subtract 5 minutes mfarch99 Linux - General 10 06-18-2014 08:07 AM
[SOLVED] How do I change the System Date format to DD/MM/YYYY on RHEL Workstatio 6.1 skinutter Linux - Newbie 3 09-28-2011 08:07 AM
[SOLVED] dd/mm/yyyy date format Ian D Linux - Newbie 4 01-11-2010 03:24 PM
Date Format dd/mm/yyyy in Suse 10.0 via Webmin collabcomp Linux - Newbie 2 09-15-2007 11:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration