Skip to content

Instantly share code, notes, and snippets.

Last active Oct 9, 2020
What would you like to do?
Example of function to try something a max number of times (and wait between retries).
function get_file() {
# Declare these local variables as integer
local -i count
local -i max_retries
local -i wait_time
local filename
# Loop until the file gets downloaded or
# the max number of retries gets reached.
until wget -q "${filename}" ; do
sleep $wait_time
count+= 1
if [ "$count" -eq "$max_retries" ]; then
# Max number of retries exceeded
echo "It was impossible to download ${filename}."
return 1
# Call the function
get_file "${URL}"

This comment has been minimized.

Copy link

@mh21 mh21 commented Oct 9, 2020

if wget would be a shell function, set -e (if enabled) would be lost. See for a hack to get around that 🙈


This comment has been minimized.

Copy link
Owner Author

@juanje juanje commented Oct 9, 2020

@mh21 Interesting... 🤔
I've always used with a command, so I haven't run into this issue yet. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment