LOCATION=$(curl -s https://api.github.com/repos/<YOUR ORGANIZTION>/<YOUR REPO>/releases/latest \
| grep "zipball_url" \
| awk '{ print $2 }' \
| sed 's/,$//' \
| sed 's/"//g' ) \
; curl -L -o <OUTPUT FILE NAME> $LOCATION
for example:
LOCATION=$(curl -s https://api.github.com/repos/csv2db/csv2db/releases/latest \
| grep "zipball_url" \
| awk '{ print $2 }' \
| sed 's/,$//' \
| sed 's/"//g' ) \
; curl -L -o csv2db.zip $LOCATION
Here is how it goes:
LOCATION=$(...)
stores the output of all the commands in the brackets in the variable $LOCATION
curl -s https://api.github.com/repos/csv2db/csv2db/releases/latest
gets the latest release from your repository, in my case github.com/csv2db/csv2db
grep "zipball_url"
grabs the line for the zipball URL of the file --> "zipball_url": "https://api.github.com/repos/csv2db/csv2db/zipball/v1.5.1",
Note, there is also a tarball_url
if you prefer the .tar.gz
file instead.
awk '{ print $2 }'
prints just the URL part of the zipball_url
line --> "https://api.github.com/repos/csv2db/csv2db/zipball/v1.5.1",
sed 's/,$//'
removes the comma ,
at the end of the line --> "https://api.github.com/repos/csv2db/csv2db/zipball/v1.5.1"
sed 's/"//g'
removes the double quotes "
from the previous output --> https://api.github.com/repos/csv2db/csv2db/zipball/v1.5.1
Once all these commands are executed, the string is stored in the $LOCATION
variable due to the surrounding $(...)
.
The next step is to download the file from that location (or, at this stage you can do anything else you like with $LOCATION
).
curl -L -o csv2db.zip $LOCATION
invokes cURL and downloads $LOCATION
into a file called csv2db.zip
. The -L
parameter is important so that cURL follows the URL, i.e. redirects on the web page, in case that the URL gets forwarded to https
or another location on the server. cURL also has an option to just store the file name under the remove file name. This can be done via -O
(uppercase O
) rather than -o
. If you are using -O
you are also advised to use -J
which tells the -O
option to use the server-specified Content-Disposition filename instead of extracting a filename from the URL.
Cool, thanks @crpb!