我有一个备份脚本(bash)。部分内容如下所示。此脚本将备份进行14天的轮换。如果要更改为30天,则必须写出30个if-then块。我确定可以用漂亮的for循环代替。那会是什么?
# step 1: delete the oldest snapshot, if it exists:
if [ -d $BACKUP_DIR/daily.14 ] ; then \
$RM -rf $BACKUP_DIR/daily.14 ; \
fi ;
# step 2: shift the middle snapshot(s) back by one, if they exist
if [ -d $BACKUP_DIR/daily.13 ] ; then \
$MV $BACKUP_DIR/daily.13 $BACKUP_DIR/daily.14 ; \
fi;
if [ -d $BACKUP_DIR/daily.12 ] ; then \
$MV $BACKUP_DIR/daily.12 $BACKUP_DIR/daily.13 ; \
fi;
if [ -d $BACKUP_DIR/daily.11 ] ; then \
$MV $BACKUP_DIR/daily.11 $BACKUP_DIR/daily.12 ; \
fi;
if [ -d $BACKUP_DIR/daily.10 ] ; then \
$MV $BACKUP_DIR/daily.10 $BACKUP_DIR/daily.11 ; \
fi;
if [ -d $BACKUP_DIR/daily.9 ] ; then \
$MV $BACKUP_DIR/daily.9 $BACKUP_DIR/daily.10 ; \
fi;
if [ -d $BACKUP_DIR/daily.8 ] ; then \
$MV $BACKUP_DIR/daily.8 $BACKUP_DIR/daily.9 ; \
fi;
if [ -d $BACKUP_DIR/daily.7 ] ; then \
$MV $BACKUP_DIR/daily.7 $BACKUP_DIR/daily.8 ; \
fi;
if [ -d $BACKUP_DIR/daily.6 ] ; then \
$MV $BACKUP_DIR/daily.6 $BACKUP_DIR/daily.7 ; \
fi;
if [ -d $BACKUP_DIR/daily.5 ] ; then \
$MV $BACKUP_DIR/daily.5 $BACKUP_DIR/daily.6 ; \
fi;
if [ -d $BACKUP_DIR/daily.4 ] ; then \
$MV $BACKUP_DIR/daily.4 $BACKUP_DIR/daily.5 ; \
fi;
if [ -d $BACKUP_DIR/daily.3 ] ; then \
$MV $BACKUP_DIR/daily.3 $BACKUP_DIR/daily.4 ; \
fi;
if [ -d $BACKUP_DIR/daily.2 ] ; then \
$MV $BACKUP_DIR/daily.2 $BACKUP_DIR/daily.3 ; \
fi;
if [ -d $BACKUP_DIR/daily.1 ] ; then \
$MV $BACKUP_DIR/daily.1 $BACKUP_DIR/daily.2 ; \
fi;
# step 3: make a hard-link-only (except for dirs) copy of the latest snapshot, if that exists
if [ -d $BACKUP_DIR/daily.0 ] ; then \
$CP -al $BACKUP_DIR/daily.0 $BACKUP_DIR/daily.1 ; \
fi;
在此发布之前,请尝试至少在Google上搜索一次。有数百个网站向您展示如何完成此任务。
—
darnir
也许吧,但是我对bash并不陌生,谷歌搜索“ bash loop”并不会产生任何效果(也许会产生任何效果,但是由于我对bash并不十分了解,所以我可能错过了它,或者没有将其挂起适用于此特定情况。不过谢谢!
—
Sparctus