0

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
3
  • @ViktorKhilin No, that syntax is fine, unless you say you can't use Bash constructs, but this is tagged "bash". Commented Jan 18, 2018 at 14:38
  • Well, you are not executing grep again. What are you expecting? That the variables are gonna change themselves magically? :) Commented Jan 18, 2018 at 14:39
  • Thanks @PesaThe - I stared at this for ages and it seems its always the small things that get missed! Long day! Commented Jan 18, 2018 at 14:48

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that's what I needed. I stared at this for ages and it seems its always the small things that get missed! Long day!
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.