CS 3307 01 Written Assignment Unit 5
CS 3307 01 Written Assignment Unit 5
Bash is the most popular and widely used shell on Linux and other Unix-based operating systems
that support it. It provides a wide range of functionalities that make it ideal for automating tasks.
These tasks can vary from routine operations encountered in daily work or environment settings
to more specialized cases that require attention to detail and repetition. In such scenarios, a well-
written Bash script can significantly facilitate the process and reduce the manual effort required.
The tasks discussed in this assignment, it can similarly benefit from Bash scripts, demonstrating.
The code of the bash script to automate the task is provided in the image below:
I used a code beautifier to generate the image of the code above so that it won't obstruct the
structure of this document. The section below explains each step of the script and what it does.
EXPLANATION
This step in the script defines three variables: URL, which stores the CSV file's download link;
DEST_DIR, which specifies the directory to save the file (set to a "reports" folder in the system's
home directory); and REPORT_NAME, which holds the file's name (annual-enterprise-survey-
2023-financial-year-provisional.csv).
Next, the script checks if the destination directory ($DEST_DIR) exists using the condition if [ ! -
d "$DEST_DIR" ]; then. The -d operator checks for the directory's existence, and the (!) negates
it, so the condition is true if the directory is missing. If the directory doesn't exist, a message is
shown, and the script creates it with mkdir -p "$DEST_DIR", where the -p option ensures any
STEP 3: Download the report file using the tool of choice (wget or curl)
Once the destination directory is ready, the script downloads the report using wget, a command-
line tool (the tool can be replaced with curl if preferred). The command wget -O
"$DEST_DIR/$REPORT_NAME" "$URL" specifies that the CSV file should be saved in the
$DEST_DIR directory with the name from $REPORT_NAME. The -O flag ensures the file is
saved at the correct location and with the correct name, rather than the current directory.
After attempting to download the report, the script checks if the download was successful by
evaluating the exit status of the wget command using the special variable “$?”. If “$?” equals 0,
the download was successful, and a success message is printed. If the exit status is not 0,
indicating an error, the script prints a failure message and exits with a non-zero status (exit 1),
This particular step exists to make sure that the file was indeed moved to the destination directory
downloaded file from the current directory to the folder specified by $DEST_DIR.
Finally, the script prints a confirmation message to notify the user that the report has been
successfully moved to the designated folder. The echo "Report has been moved to
To set up the script, there are quite some things that need to be done.
The first step is to type all the instructions in the image into a file with any name of your choice,
but the file extension must be “sh”. However, instead of creating the script from scratch, you can
download the version that I attached together with this document for this assignment. The name
The second step in the setup is to make sure that wget (or curl, if you prefer) is correctly installed
and available for use in your system. If not, then download it, using your systems package
The third step is to make the script executable by running the chmod command, this way:
chmod +x <FILE_NAME>
Where FILE_NAME is the name you selected for the file. To verify that this step was successful,
run the script to verify that it works on your system before proceeding to the next and final step.
The final step is to schedule the script to run automatically on a daily schedule. You can easily
achieve this by using the cron command, which comes installed in many major Linux
distributions. You use the cron command to create a cron job, which will take care of running the
script at the scheduled time. For more information on how to set up a cron job, check out this
as long as the URL with which the file is hosted continues to be valid, and the server hosting the
Reference:
https://opensource.com/article/17/11/how-use-cron-linux