Hungry Mind
Hungry Mind

Reputation: 226

Error on backup bash script: syntax error near unexpected token `newline'

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?

Upvotes: 0

Views: 2999

Answers (1)

Aserre
Aserre

Reputation: 5062

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.

Upvotes: 1

Related Questions