簡體   English   中英

Bash - 用給定日期計算

[英]Bash - Calculating with given date

我想在給定日期檢查 Icinga。 給定的日期有他自己的陣型,所以我只能批量更改,我試過了。

所以 Bash 必須在 fDate 比實際日期低 7 天時給我一個批評,而 Bash 必須在 fDate 比實際日期低 21 天時給我一個警告。

這是我的腳本:


#Actuall date in format from the Fortigate
LANG=en_us_8859_1 
aDate=`date +"%a %b %d %Y"`
aDateOW=`date +"%b/%d/%Y"`
#aDateOM=`date +"%d %Y"`
echo $aDate Actuall date with weekday/month
echo $aDateOW Actuall date without weekday with slash
#echo $aDateOM Actuall date without weekday/month
echo -------------------------------------------------------


#Date from Fortigate (webfilter-expliration) 
#testdate manuell declared
fDate="Sun Oct 27 2019"
fDateOW="${fDate:4:11}"
fDateOW="${fDateOW//[ ]//}"
echo $fDateOW Date from Fortigate with slash
date -d 'fDateOW 7 days'
echo $date test
#fDateOM="${fDate:8:8}"
echo $fDate Date from Fortigate with weekday/month
echo $fDateOW Date from Fortigate without weekday
#echo $fDateOM Date from Fortigate without weekday/month
echo -------------------------------------------------------


#Exit Codes Icinga
#ok=0
#warn=1
#crit=2
#unknown=3

if [[ "$aDateOW" < "$fDateOW" ]] 
then 
  echo ok
#  exit 0
elif
$fDateOW -v-7d
  [[ "$aDateOW" > "$fDateOW" ]]
then
  echo ok
elif 
  [[ "$aDateOW" == "$fDateOW" ]]
then
  echo warning
#  exit 1
elif 
  [[ "$aDateOW" > "$fDateOW" ]]
then  
  echo critical
#  exit 2
fi
echo $fDateOW

您必須將日期格式化為 unix 時間戳(或紀元)。 這是自 1970 年 1 月 1 日以來的秒數。下面是一個小示例。

#! /bin/bash

tomorrow=$(date -d "+1 days")
yesterday=$(date -d "-1 days")

t0=$(date -d "${tomorrow}" "+%s")
t1=$(date -d "${yesterday}" "+%s")

echo $t0, $t1

if [ $t0 -lt $t1 ]; then
        echo "Tomorrow is before yesterday"
else
        echo "Yesterday is before tomorrow"
fi
exit 0

請注意,紀元將在 UTC 2038-01-19 03:14:07 后一秒溢出。 如果您有興趣,請閱讀內容:-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM