BASH
Downloading Files with Progress and Retries
Efficiently download files in Bash using `curl`, including options for showing progress, retrying failed downloads, and handling redirects, ideal for fetching web resources.
#!/bin/bash
# --- Configuration ---
DOWNLOAD_URL="https://example.com/some_file.zip" # Replace with your URL
OUTPUT_FILE="downloaded_file.zip" # Desired local filename
MAX_RETRIES=5 # Max attempts for download
RETRY_DELAY=5 # Delay in seconds between retries
# --- Functions ---
log_message() {
local type=$1
local message=$2
echo "[$(date +'%Y-%m-%m %H:%M:%S')] [$type] $message"
}
download_file() {
local url=$1
local output=$2
local attempt=1
while [ $attempt -le $MAX_RETRIES ]; do
log_message INFO "Attempt $attempt of $MAX_RETRIES: Downloading $url to $output..."
# Use curl to download with progress, retries, and follow redirects
# -L: Follow redirects
# -f: Fail silently (no output on HTTP errors)
# -s: Silent mode (no progress meter or error messages)
# --retry <num>: Retry n times (curl's own retry, useful but we add our own loop for more control/logging)
# --retry-delay <sec>: Wait time between retries
# -o: Write output to file instead of stdout
# --progress-bar or -#: Show progress bar
if curl -L -f -# -o "$output" "$url"; then
log_message INFO "Download successful: $output"
return 0 # Success
else
local curl_exit_code=$?
log_message WARNING "Download failed (curl exit code: $curl_exit_code) for $url. Retrying in $RETRY_DELAY seconds..."
attempt=$((attempt + 1))
sleep $RETRY_DELAY
fi
done
log_message ERROR "Failed to download $url after $MAX_RETRIES attempts."
return 1 # Failure
}
# --- Main Script ---
log_message INFO "Script started."
if download_file "$DOWNLOAD_URL" "$OUTPUT_FILE"; then
log_message INFO "File processing can continue with $OUTPUT_FILE."
# Example: unzip "$OUTPUT_FILE"
else
log_message ERROR "Script terminated due to download failure."
exit 1
fi
log_message INFO "Script finished."
How it works: This script demonstrates how to reliably download files using `curl` with progress indication and built-in retry logic. It configures the download URL, output file, maximum retries, and delay. The `download_file` function uses a `while` loop to attempt the download multiple times, logging progress and errors. `curl` options like `-L` (follow redirects), `-f` (fail on HTTP errors), and `-#` (progress bar) are used to enhance the download process, making it resilient to transient network issues.