Skip to content

Instantly share code, notes, and snippets.

@gbraad
Last active January 19, 2025 11:40
Show Gist options
  • Save gbraad/7ee6a057197fd19fe826179c7dcb0ad5 to your computer and use it in GitHub Desktop.
Save gbraad/7ee6a057197fd19fe826179c7dcb0ad5 to your computer and use it in GitHub Desktop.
#!/bin/bash
download() {
input=$1
final_output_file=$2
# Check if the input contains a range pattern
if [[ $input =~ \[([0-9]+)-([0-9]+)\] ]]; then
# Extract the base URL and the range
base_url="${input%%_part*}"
start_part=${BASH_REMATCH[1]}
end_part=${BASH_REMATCH[2]}
# Remove any existing final output file to avoid appending to old data
rm -f "$final_output_file"
# Loop through the range and download each part, appending to the final file
for i in $(seq $start_part $end_part); do
part_url="${base_url}_part${i}"
echo "Download file $part_url and appending to $final_output_file..."
curl -s -L "$part_url" -o - >> "$final_output_file"
if [[ $? -ne 0 ]]; then
echo "Error downloading $part_url. Exiting."
return 1
fi
done
echo "Download completed and concatenated into $final_output_file."
else
# Direct download
url=$input
echo "Download $url to $final_output_file..."
curl -s -L -o "$final_output_file" "$url"
if [[ $? -ne 0 ]]; then
echo "Error downloading $url. Exiting."
return 1
fi
echo "Download completed: $final_output_file."
fi
}
# Example usage
download "https://github.com/gbraad-apps/personal-libreoffice/releases/download/latest/disk.qcow2_part[0-1]" "libreoffice.qcow2"
download "https://github.com/gbraad-apps/personal-obsidian/releases/download/latest/disk.qcow2" "obsidian.qcow2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment