我遇到了以下腳本的問題。由於我對BASH缺乏瞭解,我不能100%確定如何創建一個循環,它使用「for」來計算數組中變量的數量並循環遍歷每個變量。基本上我想要做的是讓腳本計算變量$ sqldumps中的變量數量,然後爲每個變量運行一個MySQL轉儲(也將它們記錄到數據庫中)。腳本的長版登載在底部,但這裏是低於短版:BASH問題,爲FOR循環計數數組中的變量
#! /usr/bin/env bash
echo -ne '\033]0;World of Clucky - Backup\007'
BINDIR="$(dirname "$(readlink -fn "$0")")"
cd "$BINDIR"
sqldumps=("lwc" "bans" "frisnuk_permissions" "jail" "imonies" "GriefP" "permissions" "showcase" "simpleclans")
#Copy MySQL Databases
echo "[`date '+%H:%M'`] Starting MySQL Backup" >> /home/clucky/MinecraftServers/backup.log
for i in ${#sqldumps[*]}
do
echo " ${sqldumps[i]}" >> /home/clucky/MinecraftServers/backup.log;
/opt/lampp/bin/mysqldump -u'root' -p'CENSORED' --skip-lock-tables ${sqldumps[i]} > /home/clucky/MinecraftServers/.backups/$timedate/MySQL/${sqldumps[i]}.sql;
done
echo " forum";
/opt/lampp/bin/mysqldump -u'root' -p'CENSORED' forum >> /home/clucky/MinecraftServers/.backups/$timedate/MySQL/forum.sql;
echo "[`date '+%H:%M'`] MySQL Backup Complete" >> /home/clucky/MinecraftServers/backup.log
,我有問題與特定的路線是:
for i in ${#sqldumps[*]}
我假設我爲此使用了錯誤的語法,所以如果任何人都可以給我正確的語法,那將不勝感激。謝謝!
#! /usr/bin/env bash
echo -ne '\033]0;World of Clucky - Backup\007'
BINDIR="$(dirname "$(readlink -fn "$0")")"
cd "$BINDIR"
timedate=`date '+%m.%d.%Y-%H:%M'`
sqldumps=("lwc" "bans" "frisnuk_permissions" "jail" "imonies" "GriefP" "permissions" "showcase" "simpleclans")
echo "-------------- `date '+%d-%B-%Y %H:%M'` --------------" >> /home/clucky/MinecraftServers/backup.log
echo "[`date '+%H:%M'`] Starting Minecraft Backup" >> /home/clucky/MinecraftServers/backup.log
mkdir -p /home/clucky/MinecraftServers/.backups/$timedate
mkdir -p /home/clucky/MinecraftServers/.backups/$timedate/MySQL
nice --adjustment=19 tar -zcpf /home/clucky/MinecraftServers/.backups/$timedate/Minecraft.tar.gz \
--directory /home/clucky/MinecraftServers/ \
--exclude=SkyBlockkit \
--exclude=Tekkit \
--exclude=Vanilla \
--exclude=test \
--exclude=Zflocco_LP \
--exclude=Mortuus/plugins/dynmap/web --exclude=Mortuus/plugins/AutoSaveWorld/backups --exclude Mortuus/CraftBukkitVersion --exclude=Mortuus/orebfuscator_cache --exclude=Mortuus/plugins/#Removed --exclude=Mortuus/server.log \
--exclude=Frisnuk/plugins/dynmap/web --exclude=Frisnuk/plugins/AutoSaveWorld/backups --exclude Frisnuk/CraftBukkitVersion --exclude=Frisnuk/orebfuscator_cache --exclude=Frisnuk/plugins/#Removed --exclude=Frisnuk/plugins/#AwaitingUpdate --exclude=Frisnuk/server.log --exclude=Frisnuk/Suwako_Moriya100 --exclude=Frisnuk/Uk_Shadow --exclude=Frisnuk/Whiteghost99 --exclude=Frisnuk/greenrangermatt \
--exclude=dailybackup.log \
--exclude=.backups \
--exclude=backup.log \
--exclude=backups.log \
--exclude=backupscript.sh \
--exclude=dailybackup.sh \
--exclude=.keptbackups .
echo "[`date '+%H:%M'`] Minecraft Backup Complete" >> /home/clucky/MinecraftServers/backup.log
#Purge files 3 days old
echo "[`date '+%H:%M'`] Purging Old Backups" >> /home/clucky/MinecraftServers/backup.log
find /home/clucky/MinecraftServers/.backups* -mmin +4320 -exec rm --recursive {} \;
echo "[`date '+%H:%M'`] Purging Complete" >> /home/clucky/MinecraftServers/backup.log
#Copy MySQL Databases
echo "[`date '+%H:%M'`] Starting MySQL Backup" >> /home/clucky/MinecraftServers/backup.log
for i in ${#sqldumps[*]}
do
echo " ${sqldumps[i]}" >> /home/clucky/MinecraftServers/backup.log;
/opt/lampp/bin/mysqldump -u'root' -p'CENSORED' --skip-lock-tables ${sqldumps[i]} > /home/clucky/MinecraftServers/.backups/$timedate/MySQL/${sqldumps[i]}.sql;
done
echo " forum";
/opt/lampp/bin/mysqldump -u'root' -p'CENSORED' forum >> /home/clucky/MinecraftServers/.backups/$timedate/MySQL/forum.sql;
echo "[`date '+%H:%M'`] MySQL Backup Complete" >> /home/clucky/MinecraftServers/backup.log
echo "[`date '+%H:%M'`] Daily Backup Complete" >> /home/clucky/MinecraftServers/backup.log
#Read back file size
filesize=$(ls -lah /home/clucky/MinecraftServers/.backups/$timedate | awk '{ print $5}')
echo " Total Compression Size: $filesize\n" >> /home/clucky/MinecraftServers/backup.log
謝謝您的協助!
你需要知道的數組中的變量數或可你只需要使用陣列「地圖」方法的名額? – 2013-03-13 23:16:17
真的,我真的不知道。我只需要一種方法讓腳本遍歷數組$ sqldumps中的所有變量併爲每個變量運行一次MySQL轉儲。 – Clucky 2013-03-13 23:25:51