开发者

shell programming help - best way to do a unix diff and report any differences

Shell newbie here. I'm not sure what I am doing is a good way to go about this.

Basically this is what i want to do:

  1. create a list of current directories (curr.lst)
  2. do a diff with the curr.lst and the old list (before.lst)

  3. if there is no difference, then ok no change, change the curr.lst to before.lst for next time it runs

  4. if there the diff.lst is greater than 0 bytes (meaning there is a change, then do something, mail the list of changes, etc.

Anyways, My logic isn't quite right. It could be my use of if statments.

I'm looking for help to either tweak my l开发者_如何学运维ogic, better coding practice to do this simple task. Really all I want to do is have this script run daily and check if there is changes between the old and the new, if there is a change, i want to know.

Thanks for any input, suggestions, examples.

#!/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


One thing I see is that you already execute $CHKSZ (resp. du -k ...) in line 9 of you script. The command in backticks is executed immediately.

A second problem might be that you use du -k which prints the size in kilobites. If there are only minor changes you might get a size of 0. I guess you better use du -b to get the size in bytes (if you want to do it in this way).


From the diff(1) man page:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜