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.
- Download the ZIP file (
curl -O
)
- Unzip the files to a specific location (
unzip filename.zip path/to/save
)
- Delete the .zip file
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?
OSX uses the same GNU sh/bash as Linux
#!/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/sh
is so called "shebang" line and is mandatory
BSD Tar can open a zip file and decompress through a stream. So the following will work:
curl http://example.org/file.zip | tar -xf - -C path/to/save
If you do not want change directory context, use the following script:
#!/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
}
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