我已经制作了我的小 shell 函数来解压缩特定文件夹中的最新存档,然后使用它(为了清楚起见,删除了代码),然后制作存档,测试它,如果一切正常,则删除该tmp
文件夹,否则 停下来。有时,它不起作用(存档问题我不知道何时重试它有效),所以我应该不是删除文件夹,然后停止。我的小脚本不起作用。我已经发表了一条评论,其中一句行不通,所以这是我的脚本:
mine() {
src="/mnt/myfolder"
game_folder="MyGameFolder"
src_folder="${src}/${game_folder}"
src_archive="${src}/${game_folder}.tar"
tmp="/tmp/tmpfolder"
# Expand path (don't do this if path is set by user input!):
echo "Folder tmp: '${tmp}'."
[ ! -d "${src}" ] && echo "Folder src: '${src}' not found. Exiting." && return 1
rm -rf "${tmp}"
mkdir "${tmp}"
[ ! -d "${tmp}" ] && echo "Folder tmp: '${tmp}' not found. Exiting." && return 1
cd "${tmp}"
if [ -d "${src_folder}" ]; then
# Archive folder exists -> find latest archive and uncompress:
latest="$(ls -t "${src_folder}/" | head -1)"
full_latest="${src_folder}/$latest"
if [ -f "${full_latest}" ]; then
echo Copying "${latest}"...
cp "${full_latest}" .
echo Unpacking "${latest}"...
tar xf "${latest}"
else
echo "Warn: no archive found in: '${src_folder}'"
fi
else
echo "No folder: '${src_folder}', creating folder, it's a new game..."
mkdir "${src_folder}" \
|| (echo "Unable to create '${src_folder}'" ; return 1)
fi
archive="$(date '+%Y.%m.%d-%Hh%Mm%Ss').${game_folder}.tar.bz2"
printf "\n\nMaking archive '${archive}'..."
# here's the one-liner that doesn't work:
tar cjf "${archive}" "${game_folder}/" &&
mv "${archive}" "${src_folder}/" &&
latest="$(ls -t "${src_folder}/" | head -1)"
&& tar -tf "${latest}" ||
(echo "Error tar cjf ${archive}" "${src_archive}/" && exit 1)
rm -rf "${tmp}"
cd
printf "\nDone. You can exit the shell if you want now.\n\n"
return 0
}
我应该改变什么才能让它发挥作用?