You've got a good start, but there are several issues and areas for improvement in your script. Here's a breakdown of the problems and a corrected version with explanations:
Problems:
folder_name is undefined: You try to use folder_name in mkdir -p "$DOWNLOAD_DIR/$folder_name" but it's never defined. You're trying to use download_job_name which is defined, but you need to assign it to folder_name.
- Incorrect
aria2c usage in the loop: The aria2c -c -d "$folder_name" -i "$instruction_file" line is incorrect. The -i option is for specifying a list of URLs to download from a file, not for reading URLs from the current file. You're trying to pass the entire instruction file as a single URL, which won't work.
download_from_fuckingfast uses file_url instead of url: The function download_from_fuckingfast expects the URL to be passed as $1, but you're passing $url from the loop.
[ -n "$url" ] is redundant: The [[ -z "$url" || "$url" =~ ^[[:space:]]*# ]] && continue already handles empty lines and lines starting with #. The -n check is unnecessary.
DOWNLOAD_DIR is not used in the loop: You define DOWNLOAD_DIR, but you're not using it to construct the full download path.
- Missing error handling: The script doesn't handle potential errors during the download process.
Corrected Script:
#!/bin/bash
download_from_fuckingfast() {
# $1: name of the file to download
# $2: download location for that file
file_url="$1"
download_location="$2"
# download the file
aria2c -c -d "$download_location" "$file_url"
}
DOWNLOAD_DIR="Download/"
# Loop through each file in the instruction directory
for instruction_file in "$DOWNLOAD_DIR"/*; do
# Get base name of the instruction file to use as folder name
folder_name=$(basename "$instruction_file")
# Create the folder if it doesn't exist
mkdir -p "$DOWNLOAD_DIR/$folder_name"
# Read each line from the instruction file
while IFS= read -r url; do
# Skip empty lines and comments
[[ -z "$url" || "$url" =~ ^[[:space:]]*# ]] && continue
# Download the file
echo "Downloading: $url"
download_from_fuckingfast "$url" "$DOWNLOAD_DIR/$folder_name"
done < "$instruction_file" # Redirect input to the while loop
done
Key Changes and Explanations:
folder_name=$(basename "$instruction_file"): Correctly assigns the base name of the instruction file to the folder_name variable.
download_from_fuckingfast "$url" "$DOWNLOAD_DIR/$folder_name": Passes the correct URL and the full download path to the function.
done < "$instruction_file": This is crucial. It redirects the input of the while loop from standard input to the specified file ($instruction_file). This allows the loop to read the URLs from the file.
file_url="$1": Ensures the URL is correctly assigned to the file_url variable within the function.
- Quoting: Added quotes around variables like
$DOWNLOAD_DIR, $instruction_file, $folder_name, $url, and $file_url to prevent issues with filenames containing spaces or special characters.
- Removed redundant
-n check: The [[ -z "$url" || "$url" =~ ^[[:space:]]*# ]] && continue already handles empty lines and comments.
How to Use:
- Save the script: Save the corrected script to a file (e.g.,
download_script.sh).
- Make it executable:
chmod +x download_script.sh
- Create an instruction directory: Create a directory (e.g.,
instruction_dir) and put your instruction files in it. Each instruction file should contain a list of URLs, one URL per line. You can include comments starting with #.
- Run the script:
./download_script.sh
Example instruction_dir/example.txt:
# This is a comment
https://example.com/file1.zip
https://example.com/file2.tar.gz
# Another comment
https://example.com/file3.pdf
This will create folders in the Download/ directory (if they don't exist) named example, example, and example and download the files listed in example.txt into those folders.
Further Improvements (Error Handling):
For a more robust script, you should add error handling. Here's an example of how to check the return code of aria2c:
#!/bin/bash
download_from_fuckingfast() {
# $1: name of the file to download
# $2: download location for that file
file_url="$1"
download_location="$2"
# download the file
aria2c -c -d "$download_location" "$file_url"
if [ $? -ne 0 ]; then
echo "Error downloading $file_url" >&2 # Send error to stderr
return 1 # Indicate failure
fi
}
DOWNLOAD_DIR="Download/"
# Loop through each file in the instruction directory
for instruction_file in "$DOWNLOAD_DIR"/*; do
# Get base name of the instruction file to use as folder name
folder_name=$(basename "$instruction_file")
# Create the folder if it doesn't exist
mkdir -p "$DOWNLOAD_DIR/$folder_name"
# Read each line from the instruction file
while IFS= read -r url; do
# Skip empty lines and comments
[[ -z "$url" || "$url" =~ ^[[:space:]]*# ]] && continue
# Download the file
echo "Downloading: $url"
if ! download_from_fuckingfast "$url" "$DOWNLOAD_DIR/$folder_name"; then
echo "Download failed for $url" >&2
fi
done < "$instruction_file" # Redirect input to the while loop
done
This version checks the exit code ($?) of aria2c. If it's not 0 (success), it prints an error message to standard error (>&2) and returns 1 from the function, indicating failure. The main loop also checks the return code of download_from_fuckingfast and prints an error if the download fails. This makes the script much more reliable. Using >&2 is important because error messages should go to standard error, not standard output.