从创建带有日期后缀的文件的 shell 脚本中提取文件名
我运行定期备份的工作命令:
ditto -c -k --sequesterRsrc --keepParent ~/Library/'Application Support/MyProg'/ ~/Dropbox/'Application Support/MyProg'/`date "+MP_backup_%Y-%m-%d_%H%M%S"`.zip
这将创建一个不错的文件:MP_backup_2012-02-21_133445.zip。但我想构建这个脚本并在完成后提供一个很好的咆哮通知。我如何提取生成的文件名并提供一个很好的通知,例如“备份成功完成。在 MP_backup_2012-02-21_133445.zip 进行备份”
我的代码/伪代码是这样的:
## Set the Backup path ##
BKP="~/Dropbox/Application Support/MyProg"
## Set KM Source path ##
SRC="~/Library/Application\ Support/MyProg/"
# Creat a zipped bkup with dated suffix
ditto -c -k --sequesterRsrc --keepParent "$SRC" "$BAK/`date "+MP_backup_%Y-%m-%d_%H%M%S"`.zip
## Growl a message saying the the File name is successfully created, if created else throw error ##
if [ <backup created> ]
then
growl/echo "Backup succesfully complete. Backed up at MP_backup_2012-02-21_133445.zip"
else
echo "Me no find Backup FAIL!"
fi
非常感谢将上述伪代码转换为美丽光荣的指导工作代码。
干杯。
谢谢偷看,所有提供的答案中的信息都用于提出以下最终工作解决方案:
## Set the Backup path ##
bkp="Dropbox/Application Support/My Prog"
## Set KM Source path ##
src="Library/Application Support/My Prog"
## Preferred Date Suffix ##
filename=$(date +"MP_backup_%Y-%m-%d_%H%M%S.zip")
# Creat a zipped bkup with dated suffix
ditto -c -k --sequesterRsrc --keepParent ~/"$src"/ ~/"$bkp"/"$filename"
## Growl the File name is successfully created ##
if [ -f ~/"$bkp"/"$filename" ]
then
echo "Backup successfully complete. Backed up at ~/$bkp/$filename";
else
echo "Me no find Backup FAIL!";
fi
My working command to run regular backups:
ditto -c -k --sequesterRsrc --keepParent ~/Library/'Application Support/MyProg'/ ~/Dropbox/'Application Support/MyProg'/`date "+MP_backup_%Y-%m-%d_%H%M%S"`.zip
This creates a nice file: MP_backup_2012-02-21_133445.zip. But I want to structure this script and provide a nice growl notification when complete. How would i extract the generated filename and provide a nice growl notification like "Backup succesfully complete. Backed up at MP_backup_2012-02-21_133445.zip"
My code/pseudocode goes something like this:
## Set the Backup path ##
BKP="~/Dropbox/Application Support/MyProg"
## Set KM Source path ##
SRC="~/Library/Application\ Support/MyProg/"
# Creat a zipped bkup with dated suffix
ditto -c -k --sequesterRsrc --keepParent "$SRC" "$BAK/`date "+MP_backup_%Y-%m-%d_%H%M%S"`.zip
## Growl a message saying the the File name is successfully created, if created else throw error ##
if [ <backup created> ]
then
growl/echo "Backup succesfully complete. Backed up at MP_backup_2012-02-21_133445.zip"
else
echo "Me no find Backup FAIL!"
fi
Would greatly appreciate guidance in converting above pseudocode to beautiful glorious working code.
Cheers.
Thanks peeps, info from all provided answers were used to come up with the below final working solution:
## Set the Backup path ##
bkp="Dropbox/Application Support/My Prog"
## Set KM Source path ##
src="Library/Application Support/My Prog"
## Preferred Date Suffix ##
filename=$(date +"MP_backup_%Y-%m-%d_%H%M%S.zip")
# Creat a zipped bkup with dated suffix
ditto -c -k --sequesterRsrc --keepParent ~/"$src"/ ~/"$bkp"/"$filename"
## Growl the File name is successfully created ##
if [ -f ~/"$bkp"/"$filename" ]
then
echo "Backup successfully complete. Backed up at ~/$bkp/$filename";
else
echo "Me no find Backup FAIL!";
fi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
单独生成文件名并将其存储在变量中,以便您可以在其他地方使用该变量:
Generate the filename separately and store it in a variable, so you can use that variable elsewhere:
只需将
date
调用移至变量,然后重用它即可:Just move the
date
invocation to a variable, then reuse it:也许:
perhaps: