Bash脚本Git日志时间戳比较逻辑异常问题求助
问题描述
我写了一段Bash脚本,用来检查指定文件的Git日志时间戳,当时间戳晚于「当前时间减10分钟」时通过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 ====================================================
解决方案
问题出在字符串比较时间上:Bash里用>比较字符串是按字典序判定,而非时间先后。比如Wed的字典序比Tue大,哪怕是10月26日的老旧时间,也会被错误判定为比11月1日的「10分钟前」时间晚,导致逻辑异常。
正确做法是把所有时间转成Unix时间戳(从1970年1月1日到当前的秒数),用数字大小比较,彻底避免字符串比较的歧义。
修改后的脚本
#!/bin/bash #Variables # 转成Unix时间戳(秒) NOW=$(date +%s) MINUS=$(date +%s --date '-10 min') VALUEFILES=("helm/components/admin-ui/values" "helm/components/cssr/values") #git clone xxx cd xxx #testing echo "当前时间戳: $NOW" echo "10分钟前时间戳: $MINUS" echo "当前目录: $(pwd)" ########################## #functions check_time_stamp_values () { for f in "${VALUEFILES[@]}"; do echo "====================================================" # 直接获取最新提交的时间并转成Unix时间戳,无需临时文件 latest_commit_time=$(git log -1 --format="%ci" -- "$f".yaml) latest_commit_timestamp=$(date -d "$latest_commit_time" +%s) # 转成可读格式方便调试查看 formatted_latest=$(date -d "@$latest_commit_timestamp" +"%a %b %d %H:%M:%S %Y") formatted_minus=$(date -d "@$MINUS" +"%a %b %d %H:%M:%S %Y") echo "$f.yaml 最新提交时间: $formatted_latest" if [ "$latest_commit_timestamp" -gt "$MINUS" ]; then echo "$f.yaml 最近10分钟内有修改 - 准备发送Slack告警" else echo "$f.yaml 最近10分钟内无修改 - 无需操作" fi echo "====================================================" done } ########################## # 执行检查函数 check_time_stamp_values
改动说明
- 时间戳转换:用
date +%s将当前时间、10分钟前时间直接转为Unix时间戳,从根源避免字符串比较的错误。 - 简化Git日志获取:通过
git log -1 --format="%ci"直接提取最新提交的标准时间,无需生成临时文件,提升效率。 - 数字比较逻辑:用
-gt(大于)比较时间戳的数字大小,判断逻辑完全准确。 - 保留可读时间:用
date -d "@<时间戳>"将时间戳转回人类可读格式,方便调试和日志查看。
内容的提问来源于stack exchange,提问作者Liam
相关产品推荐
相关产品推荐

