0

I am having problem finding error on bash script for handling backup:daily, monthly, yearly. Here is the script:

#!/bin/bash

echo > /home/alpha/folder/keep.txt
#writing dates of the backups that should be kept to the array

for i in {0..7}; do ((keep[$(date +%Y%m%d -d "-$i day")]++)); done
for i in {0..4}; do ((keep[$(date +%Y%m%d -d "sunday-$((i+1)) week")]++)); done
for i in {0..12}; do
        DW=$(($(date +%-W)-$(date -d $(date -d "$(date +%Y-%m-15) -$i month" +%Y-%m-01) +%-W)))
        for (( AY=$(date -d "$(date +%Y-%m-15) -$i month" +%Y); AY < $(date +%Y); AY++ )); do
        ((DW+=$(date -d $AY-12-31 +%W)))
        done
        ((keep[$(date +%Y%m%d -d "sunday-$DW weeks")]++))
done
for i in {0..30}; do
        DW=$(date +%-W)
        for (( AY=$(($(date +%Y)-i)); AY < $(date +%Y); AY++ )); do
        ((DW+=$(date -d $AY-12-31 +%W)))
        done
        ((keep[$(date +%Y%m%d -d "sunday-$DW weeks")]++))
        done

#writing the array to file keep.txt line by line
for i in ${!keep[@]}; do echo $i >> /home/alpha/folder/keep.txt; done

#delete all files that not mentioned in keep.txt
cd /home/alpha/folder
ls -1 /home/alpha/folder/ | sort /home/alpha/folder/keep.txt /home/alpha/folder/keep.txt - | uniq -u | xargs rm -rf
rm /home/alpha/folder/keep.txt

When I try to run the script, throws error message:

./back.sh: line 12: syntax error near unexpected token `newline' ./back.sh: line 12: ` done'

Where did I do wrong on the script?

3
  • Can you please elaborate more with helpful link maybe. Commented Mar 9, 2017 at 10:19
  • Sorry, it was an error on my part when testing the script, I removed the comment Commented Mar 9, 2017 at 10:20
  • 1
    Bunch of issues involving not double quoting the variables, copy paste your script under shellcheck.net and see it for yourself Commented Mar 9, 2017 at 10:20

1 Answer 1

1

Your date expression seems to misbehave inside the arithmetic context. Adding temporary variables solved your issue for me :

#!/bin/bash

echo > /home/alpha/folder/keep.txt
#writing dates of the backups that should be kept to the array

for i in {0..7}; do ((keep[$(date +%Y%m%d -d "-$i day")]++)); done
for i in {0..4}; do ((keep[$(date +%Y%m%d -d "sunday-$((i+1)) week")]++)); done
for i in {0..12}; do
        DW=$(($(date +%-W)-$(date -d $(date -d "$(date +%Y-%m-15) -$i month" +%Y-%m-01) +%-W)))
        begin=$(date -d "$(date +%Y-%m-15) -$i month" +%Y)
        for (( AY=begin; AY < $(date +%Y); AY++ )); do
        ((DW+=$(date -d $AY-12-31 +%W)))
        done
        ((keep[$(date +%Y%m%d -d "sunday-$DW weeks")]++))
done
for i in {0..30}; do
        DW=$(date +%-W)
        begin=$(($(date +%Y)-i))
        for (( AY=begin; AY < $(date +%Y); AY++ )); do
        ((DW+=$(date -d $AY-12-31 +%W)))
        done
        ((keep[$(date +%Y%m%d -d "sunday-$DW weeks")]++))
        done

#writing the array to file keep.txt line by line
for i in ${!keep[@]}; do echo $i >> /home/alpha/folder/keep.txt; done

#delete all files that not mentioned in keep.txt
cd /home/alpha/folder
ls -1 /home/alpha/folder/ | sort /home/alpha/folder/keep.txt /home/alpha/folder/keep.txt - | uniq -u | xargs rm -rf
rm /home/alpha/folder/keep.txt

However, I am unsure why the expression misbehaves inside the arithmetic block.

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

2 Comments

Array expansions should be double-quoted, for i in "${!keep[@]}"
@Inian I know this is best practice, however this doesn't affect the issue OP has. For this answer, I just copied/pasted OP's code along with my suggestions. He can refer to the shellcheck link you posted in the comments to improve his code further. As a side note, as the values stored in the array are timestamp, quotation most likely won't have any effect on the results.

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.