What is the Best Way to Perform Timestamp Comparison in Bash

前端 未结 3 2039
感动是毒
感动是毒 2021-01-31 15:36

I have an alert script that I am trying to keep from spamming me so I\'d like to place a condition that if an alert has been sent within, say the last hour, to not send another

3条回答
  •  鱼传尺愫
    2021-01-31 15:47

    By far the easiest is to store time stamps as modification times of dummy files. GNU touch and date commands can set/get these times and perform date calculations. Bash has tests to check whether a file is newer than (-nt) or older than (-ot) another.

    For example, to only send a notification if the last notification was more than an hour ago:

    touch -d '-1 hour' limit
    if [ limit -nt last_notification ]; then
        #send notification...
        touch last_notification
    fi
    

提交回复
热议问题