What is the Best Way to Perform Timestamp Comparison in Bash

前端 未结 3 2081
感动是毒
感动是毒 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:54

    Use the date command to convert the two times into a standard format, and subtract them. You'll probably want to store the previous execution time in a dotfile then do something like:

    last = cat /tmp/.lastrun
    curr = date '+%s'
    
    diff = $(($curr - $last))
    if [ $diff -gt 3600 ]; then
        # ...
    fi
    
    echo "$curr" >/tmp/.lastrun
    

    (Thanks, Steve.)

提交回复
热议问题