Bash比较时间戳



我有bash脚本,我在其中查看文件的git日志,基本上,如果文件中的时间戳大于我指定的减去时间,那么通过slack提醒我,这种方法有效,但不适用于时间戳旧得多的文件,代码如下;

#!/bin/bash
#Variables
NOW=$(date +"%a %b%Oe %H:%M:%S %Y")
MINUS=$(date +"%a %b%Oe %H:%M:%S %Y" --date '-10 min')
VALUEFILES=("helm/components/admin-ui/values" "helm/components/cssr/values")
#should run every 20minutes to catch any changes - also, once a change has been detected, get the author of the change to also send to slack notification
#git clone
#git clone xxx
cd xxx
#testing
echo "Time now with time -10mins"
echo "$NOW"
echo "$MINUS"
echo `pwd`
##########################
#functions
check_time_stamp_values () {
for f in "${VALUEFILES[@]}"; do
git log -- "$f".yaml > "$f".txt
grep "Date" "$f".txt | awk 'NR==1{ print $2, $3, $4, $5, $6 }' > "$f"-time.txt
echo "===================================================="
read -r firstline<"$f"-time.txt
if [ "$firstline" > "$MINUS" ]
then
cat "$f"-time.txt
echo "$f.yaml has changed within the last 10minutes - sending slack notification"
echo "===================================================="
else
echo "===================================================="
cat "$f"-time.txt
echo "$f.yaml has not changed in the last 10minutes - no action required"
echo "===================================================="
fi
done
}
##########################

对于以上内容,我得到以下输出;

Tue Nov 1 17:34:10 2022
Tue Nov 1 17:24:10 2022
====================================================
====================================================
Tue Nov 1 17:23:48 2022
values.yaml has not changed in the last 10minutes - no action required
====================================================
====================================================
Wed Oct 26 11:27:15 2022
helm/components/cssr/values.yaml has changed within the last 10minutes
====================================================

有人有什么建议吗?非常感谢

通过执行以下操作解决了此问题;

NOW=$(date +"%Y-%m-%d %H:%M:%S")
MINUS=$(date +"%Y-%m-%d %H:%M:%S" --date '-10 min')

更改日期的显示方式,也更改git日志的显示方式;

git log --date=format:'%Y-%m-%d %H:%M:%S' "$f".yaml > "$f".txt

现在运行良好,没有任何问题。

最新更新