通知警報腳本中的日期計算

通知警報腳本中的日期計算

我想建立帳戶到期警報,因此我需要編寫一個腳本,可以在到期前 1 個月透過電子郵件向我發出警報。我正在 Linux 上工作。

我可以得到截至2015年27月3日的到期資訊。我希望透過確定當前日期,如果 2015 年 27 月 3 日還剩 31 天,那麼我可以收到電子郵件提醒。我無法寫出正確的 if 語句。

答案1

我不太確定你被困在哪裡,因為你沒有提供大量資訊或範例,但你可以考慮以下命令:

  • chage -l userName檢查使用者密碼的到期日期
  • mail向使用者或管理員(或兩者)發送電子郵件(正如 Graeme 在他的評論中所說)

使用這兩個命令,您應該能夠編寫一個簡單的腳本來檢查密碼是否過期。

您也可以用於crontab日程安排(例如每日)。

編輯:

編輯更多資訊後,您可以嘗試如下:

# Get the current date in seconds since 1970
current_date=`date "+%s"`
# Convert the date you want to check in seconds since 1970
date_to_check=`date -d 2015/03/31 +"%s"`
# Calculate the difference in seconds
date_diff=`expr $date_to_check - $current_date`
# Check whether the difference is greater than 31 days (2678400 seconds)
if [ `expr $date_diff - 2678400` -gt 0 ]
then
    echo More than 31 days left
else
    echo Less than 31 days left
fi

相關內容