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,因为在上一次脚本运行期间将ѭ9移到了
$BEFORE
。 我认为您应该将
du
行移到
diff
行之后。     

要回复问题请先登录注册