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



壳牌新手。我不确定我现在做的是不是一个好方法。

基本上这就是我想做的:

  1. 创建当前目录列表(current .lst)
  2. 对电流进行修改。

  3. 如果没有差异,则ok不改变,改变电流。LST到之前。

  4. 如果有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

我看到的一件事是您已经执行了$CHKSZ (resp)。du -k ...)在您的脚本的第9行。

用反号括起来的命令立即执行。

第二个问题可能是您使用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

相关内容

  • 没有找到相关文章

最新更新