Here is a solution how to make cURL automatically resume interrupted downloads, from the 2013 blog post by Juan Nunez-Iglesias:
export ec=18; while [ $ec -eq 18 ]; do curl -O -C - http://site.xyz/file.zip; export ec=$?; done
It has been posted several times here, both the original version and with slight changes inside the while
loop.
The changes inside the while
loop were introduced by different people to make the script more robust.
$?
gives you the exit code of the last command in bash. So, while the exit code is 18, keep trying to download the file, maintaining the filename (-O
) and resuming where the previous download left off (-C
)." - Juan Nunez-Iglesiaswhile [ $ec -ne 0 ]
, but that feels like using a bare except in Python, which is bad. ;)" - Juan Nunez-Iglesiaswhile [ $ec -ni 0 ]
" - Ilya BelskyIn March, Helder Magalhães suggested to replace the while
loop in the original Juan's version with until
:
This solution can be further simplified using
until
unstead. See related thread.
Though I'm not really good in using cURL or in shell scripting, I would try to write it myself, but the problem to write it myself is not the script itself but the lack of understanding how to properly test it.
Could somebody show how to implement Helder's idea? And does it really make script more robust?
I use Zsh.
Here you go:
until curl ...; do
[ $? -eq 18 ] || break
done