linux shell 检查 文件 是否改变, 改变就执行$2

#!/bin/bash
function whether_changed(){
    file_path=$1
    file_old_stat="`stat ${file_path}|grep Modify`"
    while [[ true ]]; do
        sleep 2s
        if [ -f "$file_path" ]; then
            file_new_stat="`stat ${file_path}|grep Modify`"
            if [[ `echo ${file_old_stat}` == `echo ${file_new_stat}` ]]; then
                echo "### In ${check_time}s ,${file_path} does not change ###"
            else
                echo ${file_new_stat}
                echo ${file_old_stat}
                file_old_stat=${file_new_stat}
                echo "file change"
                # $2 2>&1 >/dev/null &
            fi
        else
           echo "file no exit "
        fi
    done
}

whether_changed $1 $2


你可能感兴趣的:(Linux)