Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management

Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management

Table of contents

No heading

No headings in the article.

  1. You have to do the same using Shell Script i.e using either Loops or command with start day and end day variables using arguments -

So Write a bash script createDirectories.sh that when the script is executed with three given arguments (one is directory name and the second is the start number of directories and the third is the end number of directories ) it creates a specified number of directories with a dynamic directory name.

Example 1: When the script is executed as

./createDirectories.sh day 1 90

then it creates 90 directories as day1 day2 day3 .... day90

To complete the above task, we need to follow the steps below:

Step 1: Create a file named as createDirectories.sh

Step2: Write a code to create the directory from Day1 to Day 90 as below:

Step 3: change the permission of file createdirectories.sh to the executable as below:

Step 4: Once you will change the permission of the file then execute the file.

Step 5: Output

Questions number 2: When the script is executed as ./createMovies.sh Movie 20 50 then it creates 50 directories as Movie20 Movie21 Movie23 ...Movie50

Step 1: Create the file named as createMovies.sh.

Step 2: Write a code which will create a Movies folder from 20 to 50 as below:

Step 3: Change the permissions of the file to the executable as below:

Step 4: run the file createMovies.sh as below:

Step 5: Output

Question number 3: Create a Script to back up all your work done till now.

Backups are an important part of DevOps Engineers day to Day activities The video in References will help you to understand How a DevOps Engineer takes backups.

To take a backup of the work you have done. Please create the script as below in a file named as test2.sh

Once you will write the script, change the permission of the file as below:

run the script named test2.sh as below:

Check the output for the backup file below:

  1. Cron and Crontab:

    Cron is a time-based job scheduler in Unix-like operating systems that automatically execute commands or scripts at specified times or intervals. It is a utility that runs in the background and checks the system clock at regular intervals. If any scheduled job is due to run at that time, it executes the command associated with the job.

    Crontab is a configuration file used by the cron utility to specify the commands or scripts to be executed and the times at which they should be executed. Each user on the system can have their own crontab file. The crontab file contains a list of jobs and the times when they should be executed. The format of a crontab entry specifies the minute, hour, day of the month, month, and day of the week when the command should be executed.

    Here's the basic syntax for a cron job entry:-

    Each field can be a single value, a comma-separated list of values, a range of values, or an asterisk (*), which means "any value."

    Here's what each field represents:

    • Minute (0-59)

    • Hour (0-23)

    • Day of the month (1-31)

    • Month (1-12 or Jan-Dec)

    • Day of the week (0-6 or Sun-Sat, where Sunday is 0 or 7)

    • Command to be executed

To set the script as cron we need to copy the script to /etc/cron.daily

We will create the cron job as below:

Step 1. you need to create a .sh file which has the script in it

Step 2: Change permissiom of crontest1.sh as below:

Step 3: Copy a crontest1.sh file to /etc/cron.daily (If you want to run cron job daily)

Step 4: Check Date and Time.

Step 5: Run the command crontab -e and write the script as below:

Step 6: Run crontab -l and see output as below:

Step 7: You can see the backup has been created at 18:32, Please find screen shit as below: