Shell编程-帮助自动文件比较和电子邮件任何更改



有人能帮我的shell脚本吗?我不知道我做错了什么。我已经尽我所能地把它简化了。在这个逻辑中,我遗漏了一些东西。

我的过程相当简单,但显然,我没有正确编码。

下面是我想要做的:
1. get a list (curr.lst) of directories from remote host
2. perform a `diff` on list of directories with (curr.lst) and a previous list (before.lst)
3. if there is a diff (i'm checking this by doing a `du -b` for file size) = 0 bytes, then nothing 
to report
4. if the diff is greater than 0 bytes, then email me the diff

就是这样。下面是我的测试方法:

1. run the script once
2. edit the before.lst and add/change records to get a diff
3. run again.
我输出:

$ ./tst.sh
The file size is:0 bytes   **there should be a diff because I just edited the file
No diff found

再运行一次:

$ ./tst.sh 
The file size is:83 bytes  **now it found the diff, but when I do an ls my diff.out is 0 bytes, thus I have nothing to mail
Could not exit
Detected a change
Null message body; hope that's ok
我代码:

#!/usr/local/bin/bash
TEST=/dump/stage/procs
BASE=/home/foobar/dev/mon
KEYFILE=/home/foobar/.ssh/mytestfeed-identity
BEFORE="$BASE/before.lst"
CURR="$BASE/curr.lst"
DIFFOUT="diff.out"
MAIL_RECIP="myname@foobar-inc.com"
SIZE=$(du -b "$BASE/$DIFFOUT" | cut -f 1)
ssh -i $KEYFILE user@host ls "$TEST" | perl -nle 'print $1 if m|$TEST/(S+)|' > "$CURR" 
diff -b -i $BEFORE $CURR > $BASE/$DIFFOUT 2>&1 
echo "The file size is:$SIZE bytes"       2>&1
if [  "$SIZE" = "0" ]; then
    echo "No diff found" 2>&1
    mv $CURR $BEFORE
    exit
else
    echo "Could not exit" 2>&1
fi
if [  "$SIZE" -gt 0 ]; then
        echo "Detected a change" 2>&1
        mailx -s "Changes detected in $TEST dirs" $MAIL_RECIP < $BASE/$DIFFOUT
        mv $CURR $BEFORE 
fi

您正在运行diff之前执行大小计算(du -b "$BASE/$DIFFOUT")。这意味着您将获得在脚本的最后一次运行期间生成的$DIFFOUT的大小。当前运行的$DIFFOUT的大小为0,因为您在上次脚本运行期间将$CURR移动到$BEFORE

我认为你应该把du行移到diff行之后。

相关内容

最新更新