Skip to content

Instantly share code, notes, and snippets.

@juanje
Last active Oct 9, 2020
Embed
What would you like to do?
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

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 https://gitlab.com/cki-project/pipeline-definition/-/blob/master/cki_pipeline.yml#L193 for a hack to get around that 🙈

@juanje

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