Jerome
Jerome

Reputation: 3

BASH - while loop not rechecking file

I'm struggling with a bit of basic logic and i'm hoping someone can help me out.

I've written a little whole loop to run through a file and determine if the transfer failed or was skipped.

If it wasn't then it declares "BACKUP IS GOOD" and exits cleanly. If it has failed then it declares "BACKUP IS NOT GOOD" until it reaches the max retries of 5.

The theory being that if the file that is being checked changes (anywhere in those 5 runs) that it will then change to "BACKUP IS GOOD".

The problem i'm having is that the loop isn't catching this change.

Any advice would be appreciated.

max_retries="5"
backup_count="1"

failed=$(grep -c "Transfer failed:         0" "mylog.txt")
skipped=$(grep -c "Transfer skipped:        0" "mylog.txt")

while (( $max_retries >= $backup_count ))
do
  if (( $skipped == 1 )) && (( $failed == 1 )); then
    echo "BACKUP IS GOOD"
    exit 0
elif (( $skipped != 1 )) || (( $failed != 1 )); then
    echo "BACKUP IS NOT GOOD"
    sleep 15
   (( backup_count++ ))
fi
done

Upvotes: 0

Views: 76

Answers (2)

Luis Colorado
Luis Colorado

Reputation: 12668

If you have a backup made in one system and you have to check that the backup is sent ok to another system, you had better to make some kind of test that ensures both files are equal after the transfer. One is to simply transfer two copies of it, or better, to transfer a copy of the file and a checksum (made with shasum(1) for example) At the receiving side, you can check if the shasum(1) command gives the same number, and if so, then declare that the backup has been transferred successfuly.

Upvotes: 1

marcolz
marcolz

Reputation: 2970

Your greps are evaluated before the while loop and will not change anymore.

Move the assignments to $failed and $skipped to inside the while loop, right after the do line.

Upvotes: 1

Related Questions