繁体   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