2

Hello I had a task to find remaining day counts for domain expire. The output should be number of days remain (in integer) so I tried this way I can pass domain as argument

for eg :- my domain - www.xplosa.com

script file :- ./domain-exp.sh

executing method :- ./domain-exp.sh www.xplosa.com

#!/bin/bash

target=$1

# Get the expiration date
expdate="$(whois $1 | egrep -i 'Registrar Registration Expiration Date:' | head -1)"

# Turn it into seconds (easier to compute with)
expdate=("$expdate" +%s)

# Get the current date in seconds
curdate=$(date +%s)

# Print the difference in days
echo  ($expdate - $curdate) / 86400 

This wasn't output what I expect please help me to solve this thanks in advanced.

1 Answer 1

5

First, your grep wont work if expiration date description is something like 'Expires on:' or 'Expiry Date:'. So, let's grep with a pattern like this : grep -iE 'expir.*date|expir.*on'. Of course this may have to be involved.
head -1 is for limiting result to 1 line
The grep will result in an output of like this:
Expiry Date: 2020-08-10T07:47:34Z
So we need to keep only the last word with another grep : grep -oE '[^ ]+$'

The date conversion to seconds and the final calculation have some issues. Find them in the corrected script below

#!/bin/bash
target=$1
# Get the expiration date
expdate=$(whois $1 | grep -iE 'expir.*date|expir.*on' | head -1 | grep -oE '[^ ]+$')
# 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))
2
  • Thanks a lot @cmak.fr this works like a charm , you saved me
    – soldier
    Commented Jan 6, 2020 at 6:58
  • 2
    You could get the date with a single grep: whois "$1" | grep -m1 -oPi '(expir.*date|expir.*on).*\s\K\d.+'.
    – terdon
    Commented Jan 6, 2020 at 10:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .