-2

I'm trying to get DAYS between 2 dates and if certificate expires in 30 days need to send an notification email. EndDate I'm calling from .csv file which is in EST (Thu Aug 18 18:59:59 EST 2022).

File sample3.csv contains:

$ cat sample3.csv
CertName,StartDate,EndDate
sslcertificates,Thu Dec 17 19:00:00 EST 2020,Thu Aug 18 18:59:59 EST 2022

I'm getting error like /bin/date: invalid date. Could anyone please help. I'm new for scripting.

#! /bin/bash
set +x
date=`/bin/date`
TodayDateSec="$(/bin/date "+%s")"
while IFS=, read -r CertName StartDate EndDate;
do
        if [ -z "$EndDate" ]; then
                echo "$EndDate details not exists"
        else
                echo "Name: $Name; StartDate: $StartDate; EndDate: $EndDate";
                date=`/bin/date`
                EndDateSec="$(/bin/date -d "$EndDate" +'%s')";
                DiffDays="$(( ( EndDateSec - TodayDateSec )/86400 ))"
                echo "***********$DiffDays************"
        if [[ "$DiffDays" -lt 30 ]]; then
                echo "Certificate is going to expire in $DiffDays. Please take required action"
    fi
fi

done < sample3.csv

Below is the output:

Name: CertName; StartDate: StartDate; EndDate: EndDate
/bin/date: invalid date ‘EndDate’
***********-19195************
Certificate is going to expire in -19195. Please take required action Name: sslcertificates; StartDate: Thu Dec 17 19:00:00 EST 2020; EndDate: Thu Aug 18 18:59:59 EST 2022
/bin/date: invalid date ‘Thu Aug 18 18:59:59 EST 2022’
***********-19195************
Certificate is going to expire in -19195. Please take required action

Ed Morton
  • 31,617
Cho2
  • 3
  • 1
    please, have a look at the editing help and the part about code blocks, and verify that your code is formatted correctly in the post view – ilkkachu Jul 21 '22 at 11:31
  • (1) Are you using GNU date? (2) What is the output of date --version? (3) Could you post the contents of sample3.csv in your question ? – QuartzCristal Jul 21 '22 at 17:50
  • It is a bad idea to set a variable to the same name as a command. There is no use for such variable in your script, remove both date=/bin/date` lines. – QuartzCristal Jul 21 '22 at 17:54
  • Please remove the comment character (the #) at the line #echo "Name: $Name; StartDate: $StartDate; EndDate: $EndDate"; and post the output of the edited script so we can understand what is going inside the program. – QuartzCristal Jul 21 '22 at 17:59
  • The script works correctly for me. – QuartzCristal Jul 21 '22 at 18:02
  • Name: CertName; StartDate: StartDate; EndDate: EndDate /bin/date: invalid date ‘EndDate’ *********-19195********** Certificate is going to expire in -19195. Please take required action Name: sslcertificates; StartDate: Thu Dec 17 19:00:00 EST 2020; EndDate: Thu Aug 18 18:59:59 EST 2022 /bin/date: invalid date ‘Thu Aug 18 18:59:59 EST 2022’ *********-19195********** Certificate is going to expire in -19195. Please take required action -bash-4.2$ cat sample3.csv CertName,StartDate,EndDate sslcertificates,Thu Dec 17 19:00:00 EST 2020,Thu Aug 18 18:59:59 EST 2022 – Cho2 Jul 22 '22 at 04:04
  • 1.Yes.2.date (GNU coreutils) 8.22 (3). Posted output in above comment.

    Thing is when i mentioned date in "EndDate" variable i'm getting error like 'date' command not found. If I mention /bin/date, getting error like /bin/date: invalid date ‘EndDate`. Want to fix this first. EndDateSec="$(/bin/date -d "$EndDate" +'%s')"; When i tried to convert EndDate in sec, I'm getting error like "Invalid Date" . Could you please help. I'm new for script.

    – Cho2 Jul 22 '22 at 04:14
  • Added your comments to your question. Please accept (if you agree with them) the changes. – QuartzCristal Jul 22 '22 at 13:48
  • Thanks QuartzCristal. Changes looks good accepted. – Cho2 Jul 22 '22 at 14:14
  • 1
    Please [edit] your question to show the expected output for your posted sample input so we have something we can copy/paste to test with. – Ed Morton Jul 25 '22 at 18:13
  • In general you should not post updates in comments. Add them to the question in a fashion that keeps the question easy to read and understand. (That may mean a little editing rather than just writing the word "Edit" and blobbing the text on the end.) The easier it is for people to understand your requirement the more likely you'll get a (useful) answer. – Chris Davies Jul 25 '22 at 19:16
  • Please also remember to accept the best answer with the ✔ mark next to it. This is how you show thanks to someone. You can even upvote answers too. – Chris Davies Jul 25 '22 at 19:17

2 Answers2

0

Please modify your script to include some (simple) check of data validity. Most probably the date format you are using is not being understood correctly by the command date. This could be caused for a number of reasons. If you could format your date like '2022-08-18T18:59:59' it would be a lot more portable and easier to process by other programs.

#! /bin/bash
set +x

TodayDateSec="$(/bin/date "+%s")"

while IFS=, read -r CertName StartDate EndDate; do if [ -z "$EndDate" ]; then echo "$EndDate details not exists" elif ! /bin/date -d "$EndDate" >/dev/null 2>&1; do echo "Invalid date format supplied $EndDate" elif EndDateSec="$(/bin/date -d "$EndDate" +'%s')"; DiffDays="$(( ( EndDateSec - TodayDateSec )/86400 ))" echo "*********$DiffDays**********" if [[ "$DiffDays" -lt 30 ]]; then echo "Certificate is going to expire in $DiffDays. Please take required action" fi fi done < sample3.csv

If the date format you use is not compatible with the date command you have (or the locale you use), you may try to use the date from busybox:

$ busybox date -D '%a %b %d %T %Z %Y' -d 'Thu Aug 18 18:59:59 EST 2022' +'%s'

1660863599

  • Thanks QuartzCristal. I Agree with you about date format suggested by you. But i have around 400dates in each .csv file with below format. I'm trying to resolve this issue but no luck. I'm just running below command and getting error as shown below.

    date -d"Thu Aug 18 18:59:59 EST 2022" +%s date: invalid date ‘Thu Aug 18 18:59:59 EST 2022’

    – Cho2 Jul 22 '22 at 14:41
  • Then you need to edit said csv files to re-format the date, sorry. – QuartzCristal Jul 22 '22 at 14:46
  • Please read the last paragraph on the edited answer about busybox. Just curious: What OS are you using? – QuartzCristal Jul 22 '22 at 15:02
  • I'm using RHEL and CentOS – Cho2 Jul 22 '22 at 15:21
  • 1
    Is the question answered? Are you only asking and not thanking? I don't see any up-vote or this answer selected. As you didn't vote for my previous answer from which you copied the script without even having the courtesy of giving credit where credit is due. Not nice. @Cho2 – QuartzCristal Jul 23 '22 at 14:28
  • I do not have option to install busybox in all machines, Is there any way to convert above format into seconds or Do we have command to convert above date format to required format in .csv file ? Please help thanks in advance.
    I'm newly joined in this forum. I do not have idea about this voting. apologies for that. I voted now. Thank you..
    – Cho2 Jul 24 '22 at 16:56
  • I already added yesterday a solution in your first question using awk. Do you have awk?. > The link is: https://unix.stackexchange.com/a/711026/529738 – QuartzCristal Jul 24 '22 at 17:19
0

Using a shell loop to process text is an anti-pattern, see Why is using a shell loop to process text considered bad practice?, here's how to do what your script is doing an order of magnitude more efficiently using GNU awk for time functions:

$ cat tst.sh
#!/usr/bin/env bash

awk ' BEGIN { FS="," secsInDay = 60 * 60 * 24 curDate = strftime("%Y %m %d") secs = mktime(today " 12 0 0") today = int( systime() / secsInDay ) } NR == 1 { next } $3 == "" { printf "EndDate details not exists\n" next } { printf "Name: %s; StartDate: %s; EndDate: %s\n", $1, $2, $3 split($3,d," ") mthNr = (index("JanFebMarAprMayJunJulAugSepOctNocDec",d[2])+2)/3 secs = mktime(d[6] " " mthNr " " d[3] " 12 0 0") endDay = int( secs / secsInDay ) diffDays = endDay - today printf "*********%d**********\n", diffDays } diffDays < 30 { printf "Certificate is going to expire in %d days. Please take required action\n", diffDays } ' "${@:--}"

Though this is a far better way to do what your existing script is doing, a script that produces output like this is not a good starting point for your full task of if certificate expires in 30 days need to send an notification email.. After accepting an answer to this question, ask a new question to get help on how to do THAT.

Ed Morton
  • 31,617