Last active
October 9, 2020 20:07
-
-
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 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
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 🙈