2011-04-14 6 views
0

누군가 내 쉘 스크립트를 도와 줄 수 있습니까? 나는 내가 뭘 잘못하고 있는지 알 수 없다. 내가 할 수있는 한 최선을 다했습니다. 나는이 논리로 무언가를 놓치고있다.쉘 프로그래밍 - 파일 비교 자동화 및 변경 사항 이메일 전송

내 프로세스는 매우 간단하지만 분명히 올바르게 코딩하지는 않습니다. 그것 뿐이다

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="[email protected]" 
SIZE=$(du -b "$BASE/$DIFFOUT" | cut -f 1) 
ssh -i $KEYFILE [email protected] 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 

답변

3

당신은 크기를 실행중인 계산 (du -b "$BASE/$DIFFOUT")를 실행하기 전에 diff을 실행하십시오. 즉, 스크립트를 마지막으로 실행하는 동안 $DIFFOUT 크기가 생성됩니다. 마지막 스크립트 실행 중에 $CURR$BEFORE으로 이동했기 때문에 현재 실행의 $DIFFOUT은 크기가 0입니다.

diff 행 다음에 du 행을 이동해야한다고 생각합니다.

+0

감사합니다. 나는 선언 한 변수가 그 순간에 호출되지 않을 것이라고 생각했습니다. 그것의 일 벌금. :-) – jdamae