1:需求

当所管辖的服务器磁盘空间满的时候,会发邮件给同事。将同事的联系方式保存在address_book.txt文件中,但是避免列表丢了之后重新统计的麻烦,将会在每次修改的时候备份一遍。

2:脚本

#!/bin/shcd /data/bak=address_book.txt_`date +%Y%m%d`.bak                                         //定义备份文件名称i=0                                                                                                              //定义变量,初始化为0while [ -f $bak ];do                                                                                      //循环判断文件是否存在,每更新一次,+1    bak=address_book.txt_`date +%Y%m%d`_$i.bak    let i++ done                                                                                                           //最后定出文件的名称cp /data/address_book.txt /data/$bak                                                        //备份文件vim /data/address_book.txt                                                                        //开始修改文件diff /data/address_book.txt /data/$bak > /dev/null                                      //检查此次编辑否有更新if [ $? -ne 0 ];then                                                                                        //如果有更新,就将此文件放置在同步目    /bin/cp -u /data/address_book.txt  /tmp/tongxunlu.txt                               录下,方便服务器每天取一次else    rm -rf /data/$bakfi