如何编写 BASH 脚本以在 Mac 上下载和解压缩文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8557202/
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-09 21:22:33  来源:igfitidea点击:

How do I write a BASH script to download and unzip file on a Mac?

bashmacosunzip

提问by alecwhardy

I need to create a bash script that will work on a mac. It needs to download a ZIP file of a site and unzip it to a specific location.

我需要创建一个可以在 Mac 上运行的 bash 脚本。它需要下载站点的 ZIP 文件并将其解压缩到特定位置。

  1. Download the ZIP file (curl -O)
  2. Unzip the files to a specific location (unzip filename.zip path/to/save)
  3. Delete the .zip file
  1. 下载 ZIP 文件 ( curl -O)
  2. 将文件解压缩到特定位置 ( unzip filename.zip path/to/save)
  3. 删除 .zip 文件

I need to make it so people can double-click the text file on their desktop and it will automatically run in terminal.

我需要这样做,以便人们可以双击桌面上的文本文件,它会自动在终端中运行。

How do I make it so that the user can double click the icon on the desktop and it will run? What extension does the file need?

如何使用户可以双击桌面上的图标并运行?文件需要什么扩展名?

回答by zed_0xff

OSX uses the same GNU sh/bash as Linux

OSX 使用与 Linux 相同的 GNU sh/bash

#!/bin/sh

mkdir /tmp/some_tmp_dir                         && \
cd /tmp/some_tmp_dir                            && \
curl -sS http://foo.bar/filename.zip > file.zip && \
unzip file.zip                                  && \
rm file.zip

the first line #!/bin/shis so called "shebang" line and is mandatory

第一行#!/bin/sh是所谓的“shebang”行,是强制性的

回答by ruario

BSD Tar can open a zip file and decompress through a stream.The -S flag is to follow redirects and -L to show up any errors. So the following will work:

BSD Tar 可以打开一个 zip 文件并通过流解压缩。 -S 标志是跟随重定向和 -L 显示任何错误。所以以下将起作用:

curl -SL http://example.org/file.zip | tar -xf - -C path/to/save

回答by ecwpz91

If you do not want change directory context, use the following script:

如果您不想更改目录上下文,请使用以下脚本:

#!/bin/bash

unzip-from-link() {
 local download_link=; shift || return 1
 local temporary_dir

 temporary_dir=$(mktemp -d) \
 && curl -LO "${download_link:-}" \
 && unzip -d "$temporary_dir" \*.zip \
 && rm -rf \*.zip \
 && mv "$temporary_dir"/* ${1:-"$HOME/Downloads"} \
 && rm -rf $temporary_dir
}

Usage:

用法:

# Either launch a new terminal and copy `git-remote-url` into the current shell process, 
# or create a shell script and add it to the PATH to enable command invocation with bash.

# Place zip contents into '~/Downloads' folder (default)
unzip-from-link "http://example.com/file.zip"

# Specify target directory
unzip-from-link "http://example.com/file.zip" "/your/path/here"

Output:

输出:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 17.8M  100 17.8M    0     0  22.6M      0 --:--:-- --:--:-- --:--:-- 22.6M
Archive:  file.zip
  inflating: /tmp/tmp.R5KFNvgYxr/binary