AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1200944
Accepted
soldier
soldier
Asked: 2020-01-06 21:49:26 +0800 CST2020-01-06 21:49:26 +0800 CST 2020-01-06 21:49:26 +0800 CST

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

  • 772

您好,我的任务是查找域过期的剩余天数。输出应该是剩余天数(整数)所以我尝试过这种方式我可以将域作为参数传递

例如:- 我的域 - 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 

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

scripts command-line bash 18.04
  • 1 1 个回答
  • 1330 Views

1 个回答

  • Voted
  1. Best Answer
    cmak.fr
    2020-01-06T22:32:45+08:002020-01-06T22:32:45+08:00

    首先,如果到期日期描述类似于“到期日期:”或“到期日期:”,您的 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))
    
    • 5

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve