bash 如何在 Mac 中创建批处理文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26235324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:41:22  来源:igfitidea点击:

How to create a batch file in Mac?

macosbashbatch-fileterminalapplescript

提问by David Gidony

I need to find a solution at work to backup specific folders daily, hopefully to a RAR or ZIP file.

我需要在工作中找到一个解决方案来每天备份特定文件夹,希望备份到 RAR 或 ZIP 文件。

If it was on PC, I would have done it already. But I don't have any idea to how to approach it on a Mac.

如果是在PC上,我早就做了。但我不知道如何在 Mac 上处理它。

What I basically want to achieve is an automated task, that can be run with an executable, that does:

我基本上想要实现的是一个自动化任务,它可以与一个可执行文件一起运行,它可以:

  1. compress a specific directory (/Volumes/Audio/Shoko) to a rar or zip file.

    (in the zip file exclude all *.wav files in all sub Directories and a directory names "Videos").

  2. move It to a network share (/Volumes/Post Shared/Backup From Sound).

    (or compress directly to this folder).

  3. automate the file name of the Zip file with dynamic date and time (so no duplicate file names).

  4. Shutdown Mac when finished.

  1. 将特定目录 ( /Volumes/Audio/Shoko)压缩为rar 或 zip 文件。

    (在 zip 文件中排除所有子目录和目录名称“视频”中的所有 *.wav 文件)。

  2. 将其移动到网络共享 ( /Volumes/Post Shared/Backup From Sound)。

    (或直接压缩到此文件夹)。

  3. 使用动态日期和时间自动生成 Zip 文件的文件名(因此没有重复的文件名)。

  4. 完成后关闭 Mac。

I want to say again, I don't usually use Mac, so things like what kind of file to open for the script, and stuff like that is not trivial for me, yet.

我想再说一遍,我通常不使用 Mac,所以像脚本打开什么样的文件之类的事情对我来说并不是微不足道的。

I have tried to put Mark's bash lines (from the first answer, below) in a txt file and executed it, but it had errors and didn't work.

我试图将 Mark 的 bash 行(来自下面的第一个答案)放在一个 txt 文件中并执行它,但它有错误并且没有工作。

I also tried to use Automator, but it's too plain, no advanced options.

我也尝试过使用Automator,但它太简单了,没有高级选项。

How can I accomplish this?

我怎样才能做到这一点?

I would love a working example :)

我会喜欢一个有效的例子:)

Thank You,

谢谢你,

Dave

戴夫

回答by Mark Setchell

You can just make a bashscript that does the backup and then you can either double-click it or run it on a schedule. I don't know your paths and/or tools of choice, but some thing along these lines:

您可以制作一个bash执行备份的脚本,然后您可以双击它或按计划运行它。我不知道你选择的路径和/或工具,但有些事情是这样的:

#!/bin/bash
FILENAME=`date +"/Volumes/path/to/network/share/Backup/%Y-%m-%d.tgz"`
cd /directory/to/backup || exit 1
tar -cvz "$FILENAME" .

You can save that on your Desktop as backupand then go in Terminal and type:

您可以将其保存在桌面上backup,然后进入终端并输入:

chmod +x ~/Desktop/backup

to make it executable. Then you can just double click on it - obviously after changing the paths to reflect what you want to backup and where to.

使其可执行。然后你可以双击它 - 显然是在更改路径以反映你想要备份的内容和备份的位置之后。

Also, you may prefer to use some other tools - such as rsyncbut the method is the same.

此外,您可能更喜欢使用其他一些工具——例如rsync但方法是相同的。