从创建带有日期后缀的文件的 shell 脚本中提取文件名

发布于 2025-01-07 16:25:42 字数 1636 浏览 0 评论 0原文

我运行定期备份的工作命令:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七分※倦醒 2025-01-14 16:25:42

单独生成文件名并将其存储在变量中,以便您可以在其他地方使用该变量:

filename="$BAK/`date "+MP_backup_%Y-%m-%d_%H%M%S"`.zip
ditto -c -k --sequesterRsrc --keepParent "$SRC" "$filename"

...

growl/echo "Backup succesfully complete. Backed up at $filename"

Generate the filename separately and store it in a variable, so you can use that variable elsewhere:

filename="$BAK/`date "+MP_backup_%Y-%m-%d_%H%M%S"`.zip
ditto -c -k --sequesterRsrc --keepParent "$SRC" "$filename"

...

growl/echo "Backup succesfully complete. Backed up at $filename"
ゃ人海孤独症 2025-01-14 16:25:42

只需将 date 调用移至变量,然后重用它即可:

# Create a zipped bkup with dated suffix
BACKUPFILE=`date "+MP_backup_%Y-%m-%d_%H%M%S"`
ditto -c -k --sequesterRsrc --keepParent "$SRC" "$BAK/$BACKUPFILE.zip

(...)

growl/echo "Backup succesfully complete. Backed up at $BACKUPFILE"

Just move the date invocation to a variable, then reuse it:

# Create a zipped bkup with dated suffix
BACKUPFILE=`date "+MP_backup_%Y-%m-%d_%H%M%S"`
ditto -c -k --sequesterRsrc --keepParent "$SRC" "$BAK/$BACKUPFILE.zip

(...)

growl/echo "Backup succesfully complete. Backed up at $BACKUPFILE"
小猫一只 2025-01-14 16:25:42

也许:

## Set the Backup path ##
 BKP="~/Dropbox/Application Support/MyProg"

 ## Set KM Source path ##
  SRC="~/Library/Application\ Support/MyProg/"

DATE=`date +"%Y-%m-%d_%H%M%S"`

 # Creat a zipped bkup with dated suffix
 ditto -c -k --sequesterRsrc --keepParent "$SRC" "$BAK/MP_backup_$DATE.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_$DATE.zip"
  else 
    echo "Me no find Backup FAIL!"
    fi 

perhaps:

## Set the Backup path ##
 BKP="~/Dropbox/Application Support/MyProg"

 ## Set KM Source path ##
  SRC="~/Library/Application\ Support/MyProg/"

DATE=`date +"%Y-%m-%d_%H%M%S"`

 # Creat a zipped bkup with dated suffix
 ditto -c -k --sequesterRsrc --keepParent "$SRC" "$BAK/MP_backup_$DATE.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_$DATE.zip"
  else 
    echo "Me no find Backup FAIL!"
    fi 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文