0% found this document useful (0 votes)
32 views6 pages

CS 3307 01 Written Assignment Unit 5

Provides answers to unit 5 written assignment.

Uploaded by

Nel Matrix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

CS 3307 01 Written Assignment Unit 5

Provides answers to unit 5 written assignment.

Uploaded by

Nel Matrix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Written Assignment Unit 5

Department of Computer Science

University of the People

CS 3307 - 01 – OPERATING SYSTEMS 2 - AY2025-T2

Dr. Osama Aloqaily (Instructor)

December 12th, 2024


BASH SCRIPT TO AUTOMATE THE DAILY DOWNLOAD OF SAILS REPORT

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

STEP 1: Variable definitions and URL specifications

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).

STEP 2: Checking for the existence of the destination directory

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

missing parent directories are also created.

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.

STEP 4: Verify if the download process was successful

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),

halting further execution.

STEP 5: Make sure the file is in the destination directory

This particular step exists to make sure that the file was indeed moved to the destination directory

in case of anything. The command mv "$REPORT_NAME" "$DEST_DIR/" moves the

downloaded file from the current directory to the folder specified by $DEST_DIR.

STEP 6: Task completion message

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

$DEST_DIR/$REPORT_NAME" command provides feedback, confirming that the report is in

its correct location and the process was completed successfully.


SETTING UP THE SCRIPT

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

of the script is “report_download_script.sh”.

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

manager (Apt, Pacman, Yum, etc.).

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

article by David Both (2020).


In conclusion, the script will perform a daily download of the sales report file automatically, for

as long as the URL with which the file is hosted continues to be valid, and the server hosting the

file is up and running.

Reference:

Both, D. (2020, January 2). How I use cron in Linux. Opensource.com.

https://opensource.com/article/17/11/how-use-cron-linux

You might also like