有人可以帮助我使用我的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
答案 0 :(得分:3)
在运行du -b "$BASE/$DIFFOUT"
之前,您正在执行大小计算(diff
)。这意味着您获得在上一次运行脚本期间生成的$DIFFOUT
大小。当前运行的$DIFFOUT
大小为0,因为您在上一次脚本运行期间将$CURR
移至$BEFORE
。
我认为您应该只在du
行之后移动diff
行。