我需要创建一个可以在Mac电脑上使用的Bash脚本,它需要下载一个网站的ZIP文件并将其解压到特定位置。
- 下载ZIP文件 (
curl -O
) - 将文件解压到指定位置 (
unzip 文件名.zip 保存路径
) - 删除.zip文件
我需要让用户可以双击桌面上的文本文件,它会自动在终端中运行。
如何使用户可以双击桌面上的图标并运行脚本?该文件需要什么扩展名?
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
第一行的 #!/bin/sh
被称为"shebang"行,是强制性的。
wget
。但是有安装 curl
。 - the Tin Man-sS
标志。 - Arasset -e
,并在 curl 命令中添加 --fail
,如果您希望脚本在无法下载文件时退出。 - Joscha&&
就足够了,不需要使用反斜杠。 - cevingBSD Tar可以通过流打开zip文件并进行解压缩。-L或--location标志用于跟随重定向。因此,以下命令将起作用:
curl --show-error --location http://example.org/file.zip | tar -xf - -C path/to/save
tar
有效,但在 Linux 上会出错。 - Slavik-L
参数进行操作。尝试使用curl -SL https://www.sample-videos.com/zip/10mb.zip | tar -xz - -C .
- lauksas如果您不想改变目录上下文,请使用以下脚本:
```#!/bin/bash
unzip-from-link() {
local download_link=$1; 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
}
使用方法:
# 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"
输出:
% 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
.cmd
扩展名。 - the Tin Mancurl -L http://example.org/file.zip | bsdtar -xvf - -C /path/to/save
- davidcondrey