我在處理備份的bash腳本上發現錯誤:每日,每月,每年。下面是該腳本:備份bash腳本錯誤:意外標記`新行'附近的語法錯誤
#!/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
當我嘗試運行腳本,將引發錯誤消息:
./back.sh: line 12: syntax error near unexpected token `newline' ./back.sh: line 12: ` done'
在哪裏我做錯了的腳本?
你能否詳細說明一下有用的鏈接吧。 –
對不起,這是我測試腳本時發生的一個錯誤,我刪除了註釋 – Aserre
一堆涉及不雙引用變量的問題,將您的腳本複製粘貼到http://www.shellcheck.net/並親自查看 – Inian