-1

I want to make script to show remaining days to domain expiration. I am able to get row with domain expiration date but i grep it to that format

renewal date: 2021.09.24 12:22:02

What should I do next to make date command work?

Right now Im getting date: invalid date ‘+%s’

#!/bin/bash
target=$1
# Get the expiration date
expdate=$(whois $1 | egrep -i 'Expiration|Expires on|Renewal Date| head -1 ')
echo "whoisdata:"$expdate

# Turn it into seconds (easier to compute with)
expdate=$(date -d"$expdate" +%s)
# Get the current date in seconds
curdate=$(date +%s) 
# Print the difference in days
echo $(((expdate-curdate)/86400))
Krystian
  • 3
  • 1

2 Answers2

0

You need to replace "." with "/" to allow it to be used with date and so you can use sed:

expdat=$(date -d "$(sed -n 's/renewal date:[[:space:]]+//s/\./\//gp' <<< '2021.09.24 12:22:02')" '+%s')

or

expdat=$(date -d "$(sed -n 's/renewal date:[[:space:]]+//;s/\./\//gp' <<< "$expdat")" '+%s')

Using GNU awk with built in date functions to return the days:

echo "$expdat"

renewal date:          2021.09.24 12:22:02

awk '{ dat=gensub("\\."," ","g",$3);tim=gensub(":"," ","g",$4);print (mktime(dat" "tim)-systime())/86400" days" }' <<< "$expdat"

Use gensub to convert "." to a space for the date, returning the result into the variable dat. Do the same for the time, replacing ":" returning the result into tim. Use these variables to get the epoch format date for the fed expdat variable using the mktime function, subtracting the current epoch date (attained from systime()) and divided by 86400 to get the days.

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
0

If you starting string is renewal date: 2021.09.24 12:22:02, then you can parse it with AWK :

Replace

expdate=$(whois $1 | egrep -i 'Expiration|Expires on|Renewal Date| head -1 ')

with

expdate=$(whois $1 | egrep -i 'Expiration|Expires on|Renewal Date' | awk -F' ' '{split($3,dte,/\./);split($4,hour,/:/);print dte[1]"-"dte[2]"-"dte[3]" "hour[1]":"hour[2]":"hour[3];}')
Zilog80
  • 2,534
  • 2
  • 15
  • 20
  • `#!/bin/bash target=$1 # Get the expiration date #expdate=$(whois $1 | grep -iE 'expir.*date|expir.*on|renew.*date' | head -1 | grep -oE '[^ ]+$') expdate=$(whois $1 | egrep -i 'Expiration|Expires on|Renewal Date' | awk -F' ' '{split($3,dte,/\./);split($4,hour,/:/);print dte[2]dte[3]hour[1]hour[2]dte[1]"."hour[3];}') curdate=$(date +%s) echo $(((expdate-curdate)/86400))` It looks like this right now and im getting `./domaincheck.sh: line 10: 092412222021: value too great for base (error token is "092412222021")` – Krystian Mar 31 '21 at 10:23
  • @Krystian I've corrected the date format. – Zilog80 Mar 31 '21 at 10:31
  • Awesome Thank You Sir – Krystian Mar 31 '21 at 10:45