![有人介意帮忙解决这个代码吗?](https://linux22.com/image/1518379/%E6%9C%89%E4%BA%BA%E4%BB%8B%E6%84%8F%E5%B8%AE%E5%BF%99%E8%A7%A3%E5%86%B3%E8%BF%99%E4%B8%AA%E4%BB%A3%E7%A0%81%E5%90%97%EF%BC%9F.png)
所以基本上我希望这个脚本能做的就是每 300 秒复制一个文件,但交替制作“savegame.backup1”文件,然后在接下来的 300 秒传递“savegame.backup2”,然后返回到 backup1。
我的计时器似乎没有重复,而且我不确定如何完成这个脚本。
就上下文而言,我使用的很多代码都是从谷歌搜索来的,有些是靠个人知识完成的。我无法找到我遗漏的内容。
谢谢
#!/bin/bash
GameType="Sandbox" #Sandbox, Survival, etc.
SaveName="07-01-2017_04-52-11" #Make sure you copy the folder name.
Timeout=$((5)) #Timeout in seconds. The default of 300 (5 minutes) is likely adequate.
#No need to change anything below.
GameName="Project Zomboid"
SERVICE=ProjectZomboid64 #Change this to ProjectZomboid32 if you're using 32bit java!
steam steam://rungameid/108600
# Checks to see if folder exists - if not, one will be made.
mkdir -p "$HOME/Zomboid/Saves/Backups/$SaveName"
#Loop start
while [ $Timeout -gt 0 ]; do
clear && echo -ne "Creating a backup in" $Timeout "seconds."
sleep 1
: $((Timeout--))
done
clear
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
cp "$HOME/Zomboid/Saves/$GameType/$SaveName/map_p.bin" "$HOME/Zomboid/Saves/Backups/$SaveName/map_p.bin.backup1"
#Need to alternate between the two each time it loops. Backup1 the first time, Backup2 the second, Backup1 the third...
#cp "$HOME/Zomboid/Saves/$GameType/$SaveName/map_p.bin" "$HOME/Zomboid/Saves/Backups/$SaveName/map_p.bin.backup2"
echo "$GameName is running. Backup made." #ERROR CHECKING
else
#Loop should end here, and prevent any further repeating of the loop.
read -r -p "$GameName is not detected as running, and no further backups will be made."
fi
echo "Outside of loop." #ERROR CHECKING
sleep 5s
答案1
i=1
while :;do
if [ $(pgrep $SERVICE) ];then
cp "$HOME/Zomboid/Saves/$GameType/$SaveName/map_p.bin" "$HOME/Zomboid/Saves/Backups/$SaveName/map_p.bin.backup$i"
[ $i = 1 ] && i=2 || i=1
fi
sleep 300
done