Shell编程帮助-进行Unix差异和报告任何差异的最佳方法

|| 在这里壳新手。我不确定自己在做什么是解决此问题的好方法。 基本上这就是我想要做的: 创建当前目录列表(curr.lst) 与curr.lst和旧列表(before.lst)做比较 如果没有差异,则没有变化,请在下次运行时将curr.lst更改为before.lst 如果diff.lst大于0个字节(表示有更改,则执行某些操作,将更改列表邮寄,等等)。 无论如何, 我的逻辑不太正确。这可能是我对if陈述的使用。 我正在寻找帮助来调整我的逻辑,更好的编码实践以完成此简单任务。我真正想做的就是每天运行此脚本,并检查新旧之间是否有变化,我是否想知道是否有变化。 感谢您的任何输入,建议和示例。
#!/usr/local/bin/bash
STAGE=/myproj/foo/proc
BASE=/dev/testing/scripts

BEFORE=\"$BASE/before.lst\"
CURR=\"$BASE/curr.lst\"
DIFFOUT=\"diff.out\"

CHKSZ=`du -k \"$BASE/$DIFFOUT\" | cut -f 1`

#MAIN
if [ -f $BEFORE ]; then #if we find a previous file to compare enter main loop, if not get out
          chmod 755 $BEFORE
          echo \"old list exists\"  2>&1
          echo \"get the new list and do a diff\"  2>&1
          ls \"$STAGE\" | perl -nle \'print $1 if m|$STAGE/(\\S+)|\' > \"$CURR\" #creates a file curr.lst

          #if curr.lst exists then do the diff
          if [ -f $CURR ]; then
             diff -b -i $BEFORE $CURR >> $BASE/$DIFFOUT 2>&1 #create file diff.out
          else
             echo \"command did not work, no diff.out file to compare, exiting...\" 2>&1
          exit 0
          fi

          #if file diff.out exists, check its file size, if its greater than 0 bytes then not good 
          if [ -f $BASE/$DIFFOUT ]; then
              echo \"diff.out exists, how big is it?\" 2>&1
              chmod 755 $BASE/$DIFFOUT
              $CHKSZ #run check on file size
          else
              echo \"Could not find diff.out\" 2>&1
          exit 0
          fi

          if [ $CHKSZ == \"0\" ]; then
                echo \"no changes to report\" 2>&1
                rm $BASE/$DIFFOUT #Cleanup the diff since there\'s notthing to report
                mv $CURR $BEFORE #change curr.lst to before.lst to compare next time
          else
                echo \"Detected a change\" 2>&1
                echo \"Report it\" 2>&1
          exit 0
          fi
else
echo \"No before file to compare\" 2>&1
exit 0
fi
    
已邀请:
我看到的一件事是,您已经在脚本的第9行中执行了
$CHKSZ
(分别是
du -k ...
)。反引号中的命令将立即执行。 第二个问题可能是您使用
du -k
打印以千位为单位的尺寸。如果只有很小的更改,则大小可能为0。我猜您最好使用
du -b
来获取大小(以字节为单位)(如果要以这种方式进行操作)。     
diff(1)
手册页中:
DIAGNOSTICS
       An  exit status of 0 means no differences were found, 1 means some dif-
       ferences were found, and 2 means trouble.
if ! diff -b -i \"$BEFORE\" \"$CURR\" &> /dev/null
then
  echo \"Changes were found, or an error happened\"
else
  echo \"No changes found\"
fi
    

要回复问题请先登录注册