bash练习15 (检查一个目录中的文件是否被修改)

#!/usr/bin/bash
# 检查一个目录下的所有文件是否被修改
# 用法: check_path <path>  <path>

 

check_file(){
        if [ $# -ne 2 ];then
                echo "Usage: $0 <file> <path>"
                return -1
        fi

        if [ -f $1 ];then
                FILE=$1
                if [[ $FILE =~ \/ ]];then
                        FILENAME=$(basename $FILE)
                        PATHNAME=$(dirname $FILE)
                        CURPATH=$(pwd)
                        cd $PATHNAME
                        FILE=$(pwd)/$FILENAME
                        cd $CURPATH
                else
                        FILENAME=$FILE
                        FILE=$(pwd)/$FILE
                fi
        else
                echo "$1 is not a file !"
                return -1
        fi
        #echo "File Path: $FILE"

        CURPATH=$(pwd)
        #echo "Current Path: $CURPATH"

        if [ -d $2 ];then
                DESTPATH=$2
        #       echo "DESTPATH: ${DESTPATH}"
                cd $DESTPATH
                if [ $? -ne 0 ];then
                        echo "You can not access the path: ${DESTPATH}"
                        return 1
                fi
                DESTPATH=$(pwd)
                cd ${CURPATH}
        else
                echo "$2 is not a path"
                return -1
        fi

#       echo "DESTPATH: ${DESTPATH}"

        EXIST=0
        if [ -f ${DESTPATH}/${FILENAME} ];then
                md5code_old=$(awk '{print $1}' ${DESTPATH}/${FILENAME})
                md5code_new=$(md5sum $FILE | awk '{print $1}')
        #       echo "old: $md5code_old; new:$md5code_new"
                if [ $md5code_old = $md5code_new ];then
                        EXIST=1
                fi
        fi

        _FILE=$FILE
        if [ $EXIST -eq 1 ];then
                return 0
        else
                return 1
        fi
}

get_absolutepath() {
        local curpath=$(pwd)
        local relpath=$1

        if [[ ! $relpath =~ ^\/ ]];then
                cd $relpath
                _ABSPATH=$(pwd)
        else
                _ABSPATH=$relpath
        fi

        cd $curpath
}

if [ $# -ne 2 ];then
        echo "Usage: $0 <path> <path>"
        exit 1
fi

if [ ! -d $1 -o ! -d $2 ];then
        echo "$1 or $2 is not a path or not exists"
        exit 1
fi

get_absolutepath $1; SRC=$_ABSPATH
get_absolutepath $2; DES=$_ABSPATH

srcfiles=$(ls $SRC)
#echo ${srcfiles}
#echo "SRC:$SRC"
#echo "DEST:$DES"

for item in ${srcfiles}
do
        find ${DES} -name $item &>/dev/null
        if [ $? -eq 0 ];then
                check_file ${SRC}/$item ${DES}
                if [ $? -eq 0 ];then
                        echo "$_FILE unchanged!"
                elif [ $? -eq 1 ];then
                        echo "$_FILE changed!"
                else
                        echo "$_FILE unknown!"
                fi
        else
                echo "$_FILE unknown!"
        fi
done

当前目录下:

test目录中有三个文件:123.txt hello.txt world.txt

md5codefile目录是一个空文件夹。

用bash练习13中的脚本对test目录中的每个文件生成对应一个同名文件,此文件中存储test目录中同名文件的md5值,存储到指定的md5codefile/test目录中。

执行:./md5files test md5codefile后,在md5codefile中生成了一个test文件夹,其下有三个文件,名称分别为:123.txt hello.txt world.txt

然后用./check_path test md5codefile/test检查test目录下的文件是否被修改了:

用vim编辑器,对test/123.txt文件进行修改,保存后退出。

然后再执行一次./check_path test md5codefile/test,结果提示只有123.txt文件被修改了:


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值