用于计算域到期剩余天数的 Bash 脚本

sol*_*ier 2 command-line bash scripts 18.04

您好,我有一项任务是查找域过期的剩余天数。的输出应保持的天数(整数),所以我试图这样我可以通过域作为参数

例如:- 我的域 - www.xplosa.com

脚本文件:- ./domain-exp.sh

执行方法:- ./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 
Run Code Online (Sandbox Code Playgroud)

这不是我期望的输出,请帮助我解决这个问题,谢谢。

cma*_*.fr 5

首先,如果到期日期描述类似于“到期日期:”或“到期日期:”,您的 grep 将无法工作。所以,让我们用这样的方式用grep: grep -iE 'expir.*date|expir.*on'。当然,这可能必须涉及。
head -1用于将结果限制为 1 行
grep 将导致这样的输出:
Expiry Date: 2020-08-10T07:47:34Z
所以我们只需要保留最后一个单词与另一个 grep :grep -oE '[^ ]+$'

日期转换为秒和最终计算有一些问题。在下面更正的脚本中找到它们

#!/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))
Run Code Online (Sandbox Code Playgroud)

  • 您可以使用单个 grep 获取日期:`whois "$1" | grep -m1 -oPi '(expir.*date|expir.*on).*\s\K\d.+'`。 (2认同)