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

alecwhardy picture alecwhardy · Dec 19, 2011 · Viewed 38.1k times · Source

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.

  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

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?

Answer

zed_0xff picture zed_0xff · Dec 19, 2011

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