Skip to content

Instantly share code, notes, and snippets.

@juanje
Last active October 9, 2020 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanje/0b49125c848708e0087d2433ab820907 to your computer and use it in GitHub Desktop.
Save juanje/0b49125c848708e0087d2433ab820907 to your computer and use it in GitHub Desktop.
Example of function to try something a max number of times (and wait between retries).
#!/bin/bash
URL="http://ipv4.download.thinkbroadband.com/50MB.zip"
function get_file() {
# Declare these local variables as integer
local -i count
local -i max_retries
local -i wait_time
local filename
filename="$1"
max_retries=5
wait_time=15
count=0
# 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
fi
done
}
# Call the function
get_file "${URL}"
@mh21
Copy link

mh21 commented Oct 9, 2020

if wget would be a shell function, set -e (if enabled) would be lost. See https://gitlab.com/cki-project/pipeline-definition/-/blob/master/cki_pipeline.yml#L193 for a hack to get around that 🙈

@juanje
Copy link
Author

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