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
date --version
? (3) Could you post the contents ofsample3.csv
in your question ? – QuartzCristal Jul 21 '22 at 17:50date=
/bin/date` lines. – QuartzCristal Jul 21 '22 at 17:54#
) 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:59Thing 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