linux shell脚本检测目录下的文件是否有更新

检测文件个数可以修改,打印输出可以分别修改,检测间隔可以修改

只检测一个文件

file_path0=$(cd $(dirname $0); pwd)
file_path1=${file_path0}/hardware.prom
file_path2=${file_path0}/process.prom
file_path3=${file_path0}/cdn_code.prom
file_path4=${file_path0}/cdn_base.prom
function whether_changed(){
    file_old_stat1="`stat ${file_path1}|grep Modify`"
	file_old_stat2="`stat ${file_path2}|grep Modify`"
	file_old_stat3="`stat ${file_path3}|grep Modify`"
	file_old_stat4="`stat ${file_path4}|grep Modify`"
    while [[ true ]]; do
        sleep 2s
        if [ -f "$file_path1" ]; then
            file_new_stat1="`stat ${file_path1}|grep Modify`"
            if [[ `echo ${file_old_stat1}` == `echo ${file_new_stat1}` ]]; then
                echo "### In ${check_time}s ,${file_path1} does not change ###"
            else
                echo ${file_new_stat1}
                echo ${file_old_stat1}
                file_old_stat1=${file_new_stat1}
                echo "file hardware.prom change"
                # $2 2>&1 >/dev/null &
            fi
        else
           echo "file hardware.prom no exit "
        fi

检测四个文件,变化输出1,不变化输出0

文件格式如下,jiance.py是脚本文件,其他四个是待检测文件在这里插入图片描述

#!/bin/bash

file_path0=$(cd $(dirname $0); pwd)
file_path1=${file_path0}/hardware.prom
file_path2=${file_path0}/process.prom
file_path3=${file_path0}/cdn_code.prom
file_path4=${file_path0}/cdn_base.prom
function whether_changed(){
    file_old_stat1="`stat ${file_path1}|grep Modify`"
	file_old_stat2="`stat ${file_path2}|grep Modify`"
	file_old_stat3="`stat ${file_path3}|grep Modify`"
	file_old_stat4="`stat ${file_path4}|grep Modify`"
    while [[ true ]]; do
        sleep 2s
        if [ -f "$file_path1" ]; then
            file_new_stat1="`stat ${file_path1}|grep Modify`"
            if [[ `echo ${file_old_stat1}` == `echo ${file_new_stat1}` ]]; then
                #echo "### In ${check_time}s ,${file_path1} does not change ###"
				echo "hardware.prom 0"
            else
                #echo ${file_new_stat1}
                #echo ${file_old_stat1}
                file_old_stat1=${file_new_stat1}
                echo "hardware.prom 1"
                # $2 2>&1 >/dev/null &
            fi
        else
           echo "file hardware.prom no exit "
        fi
		#第二个文件
		if [ -f "$file_path2" ]; then
            file_new_stat2="`stat ${file_path2}|grep Modify`"
            if [[ `echo ${file_old_stat2}` == `echo ${file_new_stat2}` ]]; then
                #echo "### In ${check_time}s ,${file_path2} does not change ###"
				echo "process.prom  0"
            else
                #echo ${file_new_stat2}
                #echo ${file_old_stat2}
                file_old_stat2=${file_new_stat2}
                echo "process.prom 1"
                # $2 2>&1 >/dev/null &
            fi
        else
           echo "file process.prom no exit "
        fi
		#第三个文件
		if [ -f "$file_path3" ]; then
            file_new_stat3="`stat ${file_path3}|grep Modify`"
            if [[ `echo ${file_old_stat3}` == `echo ${file_new_stat3}` ]]; then
                #echo "### In ${check_time}s ,${file_path3} does not change ###"
				echo "cdn_code.prom 0"
            else
                #echo ${file_new_stat3}
                #echo ${file_old_stat3}
                file_old_stat3=${file_new_stat3}
                echo "cdn_code.prom 1"
                # $2 2>&1 >/dev/null &
            fi
        else
           echo "file cdn_code.prom no exit "
        fi
		#第四个文件
		if [ -f "$file_path4" ]; then
            file_new_stat4="`stat ${file_path4}|grep Modify`"
            if [[ `echo ${file_old_stat4}` == `echo ${file_new_stat4}` ]]; then
                #echo "### In ${check_time}s ,${file_path4} does not change ###"
				echo "cdn_base.prom 0"
            else
                #echo ${file_new_stat4}
                #echo ${file_old_stat4}
                file_old_stat4=${file_new_stat4}
                echo "cdn_base.prom 1"
                # $2 2>&1 >/dev/null &
            fi
        else
           echo "file cdn_base.prom no exit "
        fi
		echo -e " "
    done
}

whether_changed 



输出效果图

在这里插入图片描述

将不同或者相同的保存下来为日志

在这里插入图片描述

待检测文件在这里插入图片描述

保存的日志文件

在这里插入图片描述

#!/bin/bash



#获取当前路径
#file_path0=$(cd $(dirname $0); pwd)


#文件路径
#file_path=/data1/hahaha/test/test1
file_path=/usr/local/ops_scripts_data
#日志保存路径
#log_save_path=/data1/hahaha/test/test2
log_save_path=/tmp/prom_check

CDN_CODE=${file_path}/cdn_code.prom
CDN_BASE=${file_path}/cdn_base.prom
HARDWARE=${file_path}/hardware.prom
PROCESS=${file_path}/process.prom

#输出信息到文件
#echo "node_prom_check_hardware 1" > ${log_save_path}/hardware_prom_file_status.prom
#echo "node_prom_check_hardware 0" > ${log_save_path}/hardware_prom_file_status.prom


function whether_changed(){
    file_old_stat1="`stat ${HARDWARE}|grep Modify`"
	file_old_stat2="`stat ${PROCESS}|grep Modify`"
	file_old_stat3="`stat ${CDN_CODE}|grep Modify`"
	file_old_stat4="`stat ${CDN_BASE}|grep Modify`"
    while [[ true ]]; do
        sleep 2s
        if [ -f "$HARDWARE" ]; then
            file_new_stat1="`stat ${HARDWARE}|grep Modify`"
            if [[ `echo ${file_old_stat1}` == `echo ${file_new_stat1}` ]]; then
                #echo "### In ${check_time}s ,${HARDWARE} does not change ###"
				echo "hardware.prom 0"
				echo "node_prom_check_hardware 0" >> ${log_save_path}/hardware_prom_file_status.prom
            else
                #echo ${file_new_stat1}
                #echo ${file_old_stat1}
                file_old_stat1=${file_new_stat1}
                echo "hardware.prom 1"
				echo "node_prom_check_hardware 1" >> ${log_save_path}/hardware_prom_file_status.prom
                # $2 2>&1 >/dev/null &
            fi
        else
           echo "file hardware.prom no exit "
        fi
		#第二个文件
		if [ -f "$PROCESS" ]; then
            file_new_stat2="`stat ${PROCESS}|grep Modify`"
            if [[ `echo ${file_old_stat2}` == `echo ${file_new_stat2}` ]]; then
                #echo "### In ${check_time}s ,${PROCESS} does not change ###"
				echo "process.prom  0"
				echo "node_prom_check_hardware 0" >> ${log_save_path}/process_prom_file_status.prom
            else
                #echo ${file_new_stat2}
                #echo ${file_old_stat2}
                file_old_stat2=${file_new_stat2}
                echo "process.prom 1"
				echo "node_prom_check_hardware 1" >> ${log_save_path}/process_prom_file_status.prom
                # $2 2>&1 >/dev/null &
            fi
        else
           echo "file process.prom no exit "
        fi
		#第三个文件
		if [ -f "$CDN_CODE" ]; then
            file_new_stat3="`stat ${CDN_CODE}|grep Modify`"
            if [[ `echo ${file_old_stat3}` == `echo ${file_new_stat3}` ]]; then
                #echo "### In ${check_time}s ,${CDN_CODE} does not change ###"
				echo "cdn_code.prom 0"
				echo "node_prom_check_hardware 0" >> ${log_save_path}/cdn_code_prom_file_status.prom
            else
                #echo ${file_new_stat3}
                #echo ${file_old_stat3}
                file_old_stat3=${file_new_stat3}
                echo "cdn_code.prom 1"
				echo "node_prom_check_hardware 1" >> ${log_save_path}/cdn_code_prom_file_status.prom
                # $2 2>&1 >/dev/null &
            fi
        else
           echo "file cdn_code.prom no exit "
        fi
		#第四个文件
		if [ -f "$CDN_BASE" ]; then
            file_new_stat4="`stat ${CDN_BASE}|grep Modify`"
            if [[ `echo ${file_old_stat4}` == `echo ${file_new_stat4}` ]]; then
                #echo "### In ${check_time}s ,${CDN_BASE} does not change ###"
				echo "cdn_base.prom 0"
				echo "node_prom_check_hardware 0" >> ${log_save_path}/cdn_base_prom_file_status.prom
            else
                #echo ${file_new_stat4}
                #echo ${file_old_stat4}
                file_old_stat4=${file_new_stat4}
                echo "cdn_base.prom 1"
				echo "node_prom_check_hardware 1" >> ${log_save_path}/cdn_base_prom_file_status.prom
                # $2 2>&1 >/dev/null &
            fi
        else
           echo "file cdn_base.prom no exit "
        fi
		echo -e " "
    done
}

whether_changed 



翟羽嚄 CSDN认证博客专家 算法 机器学习 图像处理
我经常会分享一些智能驾驶、自动驾驶、图像识别、嵌入式linux、嵌入式音视频开发等知识,有兴趣的朋友可以关注我的头条号——翟羽镬,ID:1661013377414159,这里会分享一些整理好的且有一定价值的文章和源码。
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页