因服务器上的程序,集团下发版本时,都不知道是什么时候下发的。有时候会将自己写的程序,给覆盖掉。因此领导要求,做一个监控程序,当目录下的文件发生变化时,将变化的文件名称,大小,修改日期,路径记录下来。方便开发人员维护。
接到这个任务,很快想到,使用ls -l找到文件当前的大小,日期。和原来的进行比对。如果当前的时间比原来的时间大,则进行记录。比对文件,想到了用awk,在这里说一下awk确实是个好东西哦,功能强大。我也只是知道使用awk可以处理这个问题。看着网上别人写的代码,慢慢的改造,终于完成了第一版的shell脚本,实现文件比对。虽然不完成,可是实现了核心的功能。核心shell脚本代码如下:
简单的介绍一下这个脚本,该脚本主要是先递归读取$FC_HOME目录下的文件,作为比对源。生成比对源文件$FC_CHECK_FILENAME,生成的内容如下:
CRBT.jar 13929 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/lib/CRBT.jar
log4j-1.2.15.jar 391834 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/lib/log4j-1.2.15.jar
debug.log 245696 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/debug.log
debug.log.2013-11-14 720658 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/debug.log.2013-11-14
debug.log.2013-11-15 773496 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/debug.log.2013-11-15
debug.log.2013-11-16 970091 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/debug.log.2013-11-16
debug.log.2013-11-17 658526 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/debug.log.2013-11-17
debug.log.2013-11-18 913028 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/debug.log.2013-11-18
debug.log.2013-11-19 893215 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/debug.log.2013-11-19
debug.log.2013-11-20 578850 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/debug.log.2013-11-20
debug.log.2013-11-21 546380 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/debug.log.2013-11-21
debug.log.2013-11-22 884870 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/debug.log.2013-11-22
debug.log.2013-11-23 776880 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/debug.log.2013-11-23
debug.log.2013-11-24 552617 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/debug.log.2013-11-24
debug.log.2013-11-25 628039 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/debug.log.2013-11-25
debug.log.2013-11-26 600807 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/debug.log.2013-11-26
debug.log.2013-11-27 707239 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/debug.log.2013-11-27
debug.log.2013-11-28 794025 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/debug.log.2013-11-28
info.log 64563 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/info.log
info.log.2013-11-14 180050 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-11-14
info.log.2013-11-15 192512 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-11-15
info.log.2013-11-16 238187 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-11-16
info.log.2013-11-17 165062 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-11-17
info.log.2013-11-18 222352 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-11-18
info.log.2013-11-19 219595 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-11-19
info.log.2013-11-20 146193 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-11-20
info.log.2013-11-21 138638 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-11-21
info.log.2013-11-22 217735 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-11-22
info.log.2013-11-23 191727 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-11-23
info.log.2013-11-24 139895 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-11-24
info.log.2013-11-25 157321 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-11-25
info.log.2013-11-26 151000 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-11-26
info.log.2013-11-27 175851 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-11-27
info.log.2013-11-28 195700 2013-12-01 14:07:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-11-28
从上面文件,我们可以清晰的看到,第一列为文件名,第二列为文件大小,第三列,第四列为修改日期,最后一列文件的路径。
同时程序还会读取(当前注释掉了,为了测试方便)$FC_UPDATE_HOME目录下的晚饭么,生成$FC_UPDATE_FILENAME,内容如下:
info.log.2013-11-26 151000 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-11-26
info.log.2013-12-27 175851 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-12-27
info.log.2013-11-28 95799 2013-12-02 14:07:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-11-28
从这个测试数据中,我们人工的和上面的比对源文件,进行比对,会发现,第一条记录没有发生变化,第二条记录为新增的,第三条记录为更新的。因此我们的脚本执行,应该是返回两条记录;脚本执行的是Shell代码中的DisposeData,具体我就不详细说明了,脚本代码中的注释的还算比较详细,细心看,应该都能看懂。执行了的结果为(生成$FC_RESULT_FILENAME文件):
127.0.0.1 otherapp 炫铃程序 M info.log.2013-11-28 95799 2013-12-02 14:07:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-11-28 195700 2013-12-01 14:07:00
127.0.0.1 otherapp 炫铃程序 A info.log.2013-12-27 175851 2013-11-29 14:38:00 /ngbss/otherapp/pengtest/CRBT/log/info.log.2013-12-27 175851 2013-11-29 14:38:00
FileCheck.rar