检查Linux文件变更Shell脚本

领导让做个脚本,大概意思就是每天检查下系统中的几个常用的文件,检查是否做过变更,然后把日志发成邮件出来。
所以先写脚本了,脚本其实很简单,大概就是先把文件备份出来,然后下次运行的时候,diff两个文件,写到日志里即可。

于是就写了一个,除了可以检查文件的变更,也可以检查某些命令输出的变更,还有shell文件输出的变更。

脚本很简单,见笑了。

View Code
#Subject:detect changes from command/file/shell
#Author:zhaofei
#Version:1.0 2013.1.23
#History:None
#
file_list="/etc/fstab /etc/passwd"
cmd_list="mount"
shell_list="computer_item.sh"
shell_root='/script/test'
date >> $shell_root/changes.log
for i in $file_list
do
        file_name=$(echo $i | sed 's/.*\///g')
        cp $i $shell_root/$file_name.new
        echo $file_name >> $shell_root/changes.log
        diff $shell_root/$file_name.new $shell_root/$file_name.old >> $shell_root/changes.log
        cp $shell_root/$file_name.new $shell_root/$file_name.old
        rm $shell_root/$file_name.new
done
for j in $cmd_list
do
        $j 1>$shell_root/$j.new
        echo $j >> $shell_root/changes.log
        diff $shell_root/$j.new $shell_root/$j.old >> $shell_root/changes.log
        cp $shell_root/$j.new $shell_root/$j.old
        rm $shell_root/$j.new
done
for k in $shell_list
do
        sh $shell_root/$k > $shell_root/$k.new
        echo $k >> $shell_root/changes.log
        diff $shell_root/$k.new $shell_root/$k.old >> $shell_root/changes.log
        cp $shell_root/$k.new $shell_root/$k.old
        rm $shell_root/$k.new
done

 

 

 

posted @ 2013-01-23 14:19  zhaofei_001  阅读(532)  评论(0编辑  收藏  举报